Llama.new
Llama.new

Reputation: 357

monitoredRegions empty even though startMonitoringForRegion:desiredAccuracy: is called

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.

Upvotes: 4

Views: 1780

Answers (3)

Austen Green
Austen Green

Reputation: 504

If you look in CLLocationManager.h, the comments in the header for startMonitoringForRegion:desiredAccuracy: state that

If a region with the same identifier is already being monitored for this application, it will be removed from monitoring. This is done asynchronously and may not be immediately reflected in monitoredRegions.

Therefore, 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 of locationManager: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.

Upvotes: 3

Bill Burgess
Bill Burgess

Reputation: 14154

If you are just going by your NSLog, it probably isn't going to work. [locationManager monitoredRegions] returns an NSSet of CLRegions. They won't display to your log that way. Try this:

NSSet *setOfRegions = [locationManager monitoredRegions];
for (CLRegion *region in setOfRegions) {
   NSLog (@"region info: %@", region);
}

Upvotes: 1

Serg Shiyan
Serg Shiyan

Reputation: 95

First of all, you should be sure, that your app has a permission to use LocationManager. Check it when you alloc your manager.

[CLLocationManager authorizationStatus];

I had the same trouble when start app and decline a permission. And after deleting and rebuilding app. I had a flag, that user didn't accept it. Turn it on.

Upvotes: 1

Related Questions