4

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.

Llama.new
  • 357
  • 8
  • 23
  • You will probably get better answers if you post some code so everyone can see what your issue might be. Regions can be a little finicky, but they aren't terribly difficult to get going. – Bill Burgess Jan 25 '12 at 20:49

3 Answers3

3

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.

Austen Green
  • 504
  • 3
  • 6
  • I have got the code to work but have also discovered what you mentioned: that the region is not immediately reflected. What I did was to use the call the start monitoring earlier and the code worked. – Llama.new Mar 20 '12 at 08:02
1

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.

Natali
  • 2,934
  • 4
  • 39
  • 53
  • LocationManager is working as I was able to determine my current location etc. I just dont seem to be able to start monitoring any regions even though I specifically called the method – Llama.new Jan 26 '12 at 08:46
1

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);
}
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86