6

Is there a way to prevent CLLocationManager from persisting monitored regions between launches? Every time the app is launched I need to add a new set of monitored regions and the old ones are no longer useful. Is there a way to either prevent them from persisting or clear all of the old ones at launch time?

charleyh
  • 2,033
  • 2
  • 20
  • 32

2 Answers2

5

Of course you can clear all the regions currently monitored:

+(void)clearRegionWatch
{
    for(CLRegion *region in [[WGLocation shared].locationManager monitoredRegions]){
        [[WGLocation shared].locationManager stopMonitoringForRegion:region];
    }
}

If you had a specific identifier that you wanted to remove:

+(void)clearRegionWatchForKey:(NSString *)key
{
    for(CLRegion *region in [[WGLocation shared].locationManager monitoredRegions]){
        if([region.identifier isEqualToString:key]){
            [[WGLocation shared].locationManager stopMonitoringForRegion:region];
        }
    }
}

You can copy the internals of the function into an appropriate place in your application. I've copied them from my shared manager class.

William George
  • 6,735
  • 3
  • 31
  • 39
  • I've found this to be unreliable. Sometimes it stops monitoring all of the regions, other times it only stops monitoring some of them. – charleyh Aug 06 '14 at 01:54
  • Interesting, in that case, have you considered reusing the same identifiers? This will overwrite the regions that "are no longer useful" – William George Aug 06 '14 at 21:59
2

In SWIFT 4 you can stop all the regions from being monitored like

let monitoredRegions = locationManager.monitoredRegions

for region in monitoredRegions{
    locationManager.stopMonitoring(for: region)
}
SUMIT NIHALANI
  • 387
  • 1
  • 10