4

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

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

Thats how I create 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 am checking for those 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 set of code works perfectly on iOS 7.

But when I run the same in iOS 8, when I start monitoring, the delegate method didStartMonitoringForRegion is getting called. But didEnterRegion is never called when I enter the region.

Is there anything that I should take extra care for iOS 8?

1

2 Answers 2

4

Region monitoring requires Always authorization from user. That's new for iOS8.

2

In iOS8 in addition of the code

 [self.locationManager requestWhenInUseAuthorization];

and add a key named "NSLocationWhenInUseUsageDescription" to plist with a message to be displayed in the prompt, to location when in foreground。

you also have to add this code

 [self.locationManager startUpdatingLocation]; 

after

[self.locationManager requestWhenInUseAuthorization];
3
  • That is already added. In my question also I have shared my plist
    – iOS
    May 21, 2015 at 6:32
  • Have you added this code [self.locationManager startUpdatingLocation]; after [self.locationManager requestWhenInUseAuthorization]; If not ,try it.
    – ChenYilong
    May 21, 2015 at 6:35
  • Yes. startUpdatingLocation was also already added and the corresponding delegates are getting called.
    – iOS
    May 21, 2015 at 7:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.