ios - iOS 8 中的 didEnterRegion - 区域监控

标签 ios objective-c iphone core-location cllocationmanager

我正在为区域监控编写原型(prototype)类型。下面是我的一组代码

        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager requestWhenInUseAuthorization];//commented for iOS 7

这就是我创建 locationManager 的方式

在plist中,

    <key>NSLocationAlwaysUsageDescription</key>
    <string>Location is required to find out where you are</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Location is required to find out where you are</string>

然后

-(BOOL) checkLocationManager
    {
        if(![CLLocationManager locationServicesEnabled])
        {
            [self showMessage:@"You need to enable Location Services"];
            return  FALSE;
        }
        if(![CLLocationManager isMonitoringAvailableForClass:[CLRegion class]])
        {
            [self showMessage:@"Region monitoring is not available for this Class"];
                    return  FALSE;
        }
        if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied ||
           [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted  )
        {
            [self showMessage:@"You need to authorize Location Services for the APP"];
            return  FALSE;
        }
        return TRUE;
    }

我正在检查那些条件

-(void) addGeofence:(NSDictionary*) dict
{

    CLRegion * region = [self dictToRegion:dict];
    [locationManager startMonitoringForRegion:region];
}
- (CLRegion*)dictToRegion:(NSDictionary*)dictionary
{
    NSString *identifier = [dictionary valueForKey:@"identifier"];
    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    if(regionRadius > locationManager.maximumRegionMonitoringDistance)
    {
        regionRadius = locationManager.maximumRegionMonitoringDistance;
    }

    NSString *version = [[UIDevice currentDevice] systemVersion];
    CLRegion * region =nil;

    if([version floatValue] >= 7.0f) //for iOS7
    {
        region =  [[CLCircularRegion alloc] initWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:identifier];
    }
    else // iOS 7 below
    {
        region = [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                       radius:regionRadius
                                                   identifier:identifier];
    }
    return  region;
}

这组代码在 iOS 7 上完美运行。

但是当我在 iOS 8 中运行相同的程序时,当我开始监视时,将调用委托(delegate)方法 didStartMonitoringForRegion。但是当我进入该区域时,didEnterRegion 从未被调用。

对于 iOS 8,有什么我应该特别注意的吗?

最佳答案

区域监控需要用户授权始终。这是 iOS8 的新功能。

关于ios - iOS 8 中的 didEnterRegion - 区域监控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30365637/

相关文章:

objective-c - NSSearchField 结果菜单

ios - 在发送到 AVAssetWriter 之前保存 CMSampleBufferRef

iPhone:OpenAL 和 AudioToolbox 泄露

iphone - 在启用弧的项目iOS中添加非弧库

ios - 查看 Controller 在创建 segue 时出错

ios - 同时连接多个 BLE 设备 iOS

ios - 带 iAd 的应用程序和不带广告的应用程序应该在不同的项目中吗?

IOS:iPad 上的 AlertView 和键盘问题

iphone - 每天执行一次任务

iphone - 如何同步不同调度队列中的任务?