I am developing a iPhone app running on iOS5, and am unable to set up geofences when I call the startMonitoringForRegion:desiredAccuracy: method on click of a button.
It works fine in the simulator when I print out the regions in monitoredRegions, but when running on an actual iPhone 4, the monitoredRegions is always empty. Expectedly, the didEnterRegion: and didExitRegion: methods are not called as well.
Another puzzling fact is that on BOTH the simulator and the iPhone 4 device, the CLLocationManagerDelegate method didStartMonitoringForRegion: is never called as well.
Would appreciate some help here, thank you!
EDIT:
this is method that I call on click of a button:
-(void) queueGeofence: (CLLocationCoordinate2D)selectedBranch userCoordinate:(CLLocationCoordinate2D)userCoordinate radius: (CLLocationDegrees)radius {
geofence = [[CLRegion alloc] initCircularRegionWithCenter:selectedBranch radius:radius identifier:@"geofence"];
CLLocationAccuracy acc = kCLLocationAccuracyNearestTenMeters;
[locationManager startMonitoringForRegion:geofence desiredAccuracy:acc];
[CLLocationManager regionMonitoringEnabled];
NSLog([CLLocationManager regionMonitoringEnabled] ? @"regionMonitoringEnabled:Yes" : @"regionMonitoringEnabled:No");
NSLog([CLLocationManager regionMonitoringAvailable] ? @"regionMonitoringAvailable:Yes" : @"regionMonitoringAvailable:No");
NSLog(@"LOCATIONMANAGER monitored regions: %@", [locationManager monitoredRegions]});
}
Region monitoring is both enabled and available, but monitoredRegions is still giving me back nothing.
If you look in CLLocationManager.h, the comments in the header for
startMonitoringForRegion:desiredAccuracy:state thatTherefore, you shouldn’t necessarily expect that
[locationManager monitoredRegions]would include your newly added region since it is added asynchronously.Are you implementing the delegate method for
locationManager:monitoringDidFailForRegion:withError:? Maybe that’s getting called instead oflocationManager:didStartMonitoringForRegion:. Also note that a region with the same identifier as an existing region will get removed, so you might be running into some unexpected problems because you’re reusing"geofence"as your identifier.