DidEnterRegion in iOS 8 - Region Monitoring

I am writing a prototype type for monitoring a region. Below is my code set

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

This is how I create a locationManager

In 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>

Then

-(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;
    }

I check these conditions

-(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;
}

This code set works great on iOS 7.

But when I run the same in iOS 8, when I start monitoring, the delegation method didStartMonitoringForRegionis called. But it didEnterRegionnever gets called when I enter the area.

Is there anything I should focus on iOS 8?

+4
source share
2 answers

To monitor the region always requires authorization from the user. This is new for iOS8.

+4
source

In iOS8, besides code

 [self.locationManager requestWhenInUseAuthorization];

"NSLocationWhenInUseUsageDescription", , , .

 [self.locationManager startUpdatingLocation]; 

[self.locationManager requestWhenInUseAuthorization];
+2

Source: https://habr.com/ru/post/1589134/


All Articles