Question

I have an iOS app that uses the CLLocationManager to monitor regions and to get GPS updates. Most of the time, I want my app to continue tracking the cellphone when it goes in background or even when it gets killed, and it works well (I can still see the small arrow in the status bar after my app gets killed). The problem is that I am not able to stop monitoring the regions and GPS updates after my app has been restarted by the Location Services.

When my app gets restarted by the Location Services, I instanciate the CLLocationManager and then call its methods stopRangingBeaconsInRegion and stopUpdatingLocation before setting its delegate to nil and itself to nil.

Thanks to NSLogger, I can see that my callbacks are no longer called, but the small arrow in the status bar stays there (and my app is the only one that I allowed to use the Location Services from the settings menu).

What did I miss? Is there a way to know what still uses the Location Services in my app?

Was it helpful?

Solution

When you call stopRangingBeaconsInRegion, where are you getting the list of regions? The proper way to do this is like below:

for (CLRegion *monitored in [self.locationManager monitoredRegions]) {
    NSLog(@"Stopping monitoring on: %@", monitored.identifier);
    [self.monitoringLocationManager stopMonitoringForRegion:monitored];
}

for (CLBeaconRegion *region in [self.locationManager rangedRegions]) {
    NSLog(@"Stopping ranging on: %@ ", region.identifier);
    [self.rangingLocationManager stopRangingBeaconsInRegion:region];
}

OTHER TIPS

I finally found that I missed to remove some of my numerous regions. The easy way to avoid this mistake is to retrieve the list of regions monitored with the property monitoredRegions and call stopRangingBeaconsInRegion for each of them. I also forgot to call stopMonitoringSignificantLocationChanges (I didn't know that my app was using it, since I am modifying the app of a former colleague).

How do you know that it is your app that is using the location services? The small arrow appears if the iOS itself is using the location services in the background.

if you did call stopRangingBeaconsInRegion and stopUpdatingLocation before setting the delegate to nil and you are not getting any callbacks then your app is not using the location services.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top