Iphone CLLocationmanager System health monitoring not called

I am trying to use monitoring regions to track whether users have visited landmarks. the location manager is initialized in the view manager along with mapkit

in viewdidload of a view controller:

if (self.locationManager == nil) { // NSLog(@"creating location manager"); self.locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; locationManager.distanceFilter = kCLDistanceFilterNone; } NSSet* set=[locationManager monitoredRegions]; if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled]) { NSLog(@"region monitoring okay"); NSLog(@"monitored regions: %@",set); } 

I get NSLogs “area monitoring is OK” and in all regions correctly.

adding areas is done like this:

 double metres=20.0; CLLocationDistance dist=metres; CLLocationAccuracy acc=1.0; CLRegion *reg=[[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:dist identifier:landmarkObj.landmarkName]; [locationManager startMonitoringForRegion:reg desiredAccuracy:acc]; 

but all callbacks do not start

  - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Entered" message:region.identifier delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exited" message:region.identifier delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { NSLog(@"started monitring for region: %@",region); } - (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { NSLog(@"%@",error); } 

location updating, however, works great.

 [locationManager startUpdatingLocation]; 

calls the didUpdateToLocation callback as expected

Update: used didUpdatToLocation to monitor regions. it’s still interesting to know why this will not work, it seems that some of them have been successful in monitoring the region.

+6
source share
5 answers

area tracking material is designed to track a location with a low degree of granularity and runs at cell boundaries, so if you don’t cross the cell border, you will never check your regions. I had the same problems and researched this, and on another website there was a comment about this that pointed to this apple forum:

https://devforums.apple.com/message/251046#251046

If you read all the comments, you will understand why this does not work.

I try to work where I define my own NSS files to contain tracked CLRegions and busy CLRegions, and then when I get locationManager: didUpdateToLocation: fromLocation: callback, I check all the regions in my tracked set to see NOT in the inRegions set but now i'm in the tracked area (add to inRegions and call back with enterRegion), or if i was inRegion but now no (remove from inRegions and call back with exitRegion). Now the work is going on.

+3
source

Have you installed CLLocationManagerDelegate in your ViewController?

 @interface Maps : UIViewController <CLLocationManagerDelegate>{ ... } 
0
source

Try using the Debug → Location → Freeway drive to simulate, I have a similar problem, but I created several regions, and didEnterRegion starts up with more than 1,000 meters ... I don’t know the path .. I installed:

 locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; locationManager.distanceFilter = kCLDistanceFilterNone; 
0
source

What do you put in the center coordinates? This part is missing in your example. I saw other examples in which they are located, does not fill it with a sufficiently accurate coordinate in combination with the radius of the region for which it has ever been triggered. You should have entered a fairly accurate coordinate (probably 5 or 6 decimal places) if you want to run with a radius of 20 meters.

Everything else looks pretty good.

0
source

You also need NSLocationAlwaysUsageDescription in your plist, and you need to call [locationManager requestAlwayAuthorization] in your application. NSLocationWhenInUseUsageDescription will disable region monitoring.

0
source

Source: https://habr.com/ru/post/904222/


All Articles