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