6

I am using location services i.e. region monitoring in my iOS app, This is my code

//this for creating region
-(void)createRegion
{
    [dictionary setValue:@"23 St, New York" forKey:@"title"];
    [dictionary setValue:[NSNumber numberWithDouble:40.742878] forKey:@"latitude"];
    [dictionary setValue:[NSNumber numberWithDouble:-73.992821] forKey:@"longitude"];
    [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"];

    [regionArray addObject:[self mapDictionaryToRegion:dictionary]];
    [self initializeRegionMonitoring:regionArray];
}
- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary {
    NSString *title = [dictionary valueForKey:@"title"];

    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:title];
}

- (void) initializeRegionMonitoring:(NSArray*)geofences {
    if(![CLLocationManager regionMonitoringAvailable]) {
       // [self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(CLRegion *geofence in geofences) {
        [_locationManager startMonitoringForRegion:geofence];
    }
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"entered region %@",region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"exited region %@",region.identifier);
}

It works fine when app is in foreground. It shows me log :"entered region.." and "exited region ..", but when app goes to background then same logs are printed twice in just fraction of second, i.e. delegate methods called twice, that i don't need, is there any way to avoid calling 2 times? or is am doing any mistake while creating or monitoring regions ? please help me .. thanks in advance ..

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
prabhu
  • 684
  • 11
  • 27
  • Unrelated to the core issue but... [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radious"]; Should be: [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"]; You have a typo on "radius" – KDM Dec 28 '12 at 17:51

1 Answers1

9

I believe this question is a duplicate of my own. I think there is a bug with Apple's region monitoring APIs. I have filed a radar for this issue with Apple already, but have not heard anything back as of yet.

One way I get around this is by saving the timestamp in the didEnter and didExit methods and if the methods fire within 10 seconds of the saved timestamp, just skip the method as a dupe.

If anyone is interested, I have a project on github showing this problem in action.

https://github.com/billburgess/GeofenceBug

Feel free to file another radar as that is the only way Apple will notice the issue and take action. The radar number is 12452255 - Duplicate Delegate calls for region monitoring.

Here is the open radar link with the information if you would like to dupe this radar. http://openradar.appspot.com/radar?id=2484401

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
  • ok, thanks Bill Burgess. We dont know when apple will remove that bug, but still that we have to remove that bug manually, definitely it exceeds our efforts. – prabhu Dec 29 '12 at 10:04
  • I think you just need to rate limit the delegate methods. Since they fire together immediately, it is safe to limit the calls to once every 5 seconds or more depending on your usage. In some cases once a minute is acceptable. Good luck. – Bill Burgess Dec 29 '12 at 19:19
  • Did anyone verified this issue on iOS 8? Is it solved? – Amit Dec 08 '14 at 12:51
  • I honestly haven't tried to test and see. I'll maybe take some time and open up my old radar test project and see what happens. – Bill Burgess Dec 08 '14 at 15:11
  • Issue is still there. But didEnter and didExit fire one after the other. Therefore workaround is checking the time stamp as suggested by @BillBurgess – TharakaNirmana Mar 23 '15 at 11:05
  • a tangent question. If I start monitoring for a region of 50meters and make `notifyOnExit = true` and then get a `didExitRegion` callback, and then move 500meters which still I'll be out of the region...would I get **another** callback? OR I would only get a callback if moved back into the region and exited again? – mfaani Jul 22 '17 at 11:02
  • In theory you could get a 2nd notify. The reality is that 50m is too small and you might not get the notify on exit event until 500-1000m. Apple pads your region to an internal minimum not matter what you set it to for exit events. But may work on the scale of kilometers in theory. – Bill Burgess Jul 23 '17 at 01:56