didEnterRegion in iOS 8 - 区域监控

didEnterRegion in iOS 8 - Region Monitoring

我正在编写一个用于区域监控的原型类型。下面是我的一组代码

        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 相同时,当我开始监视时,委托方法 didStartMonitoringForRegion 被调用。但是当我进入该区域时,didEnterRegion 从未被调用。

iOS8 有什么需要特别注意的吗?

在iOS8中添加代码

 [self.locationManager requestWhenInUseAuthorization];

并在 plist 中添加一个名为 "NSLocationWhenInUseUsageDescription" 的键,在提示中显示一条消息,在前台时定位。

您还必须添加此代码

 [self.locationManager startUpdatingLocation]; 

之后
[self.locationManager requestWhenInUseAuthorization];

区域监控需要用户Always授权。这是 iOS8 的新内容。