Can you stop regions from persisting between launches with CLLocationManager?
Asked Answered
R

2

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?

Rora answered 5/8, 2014 at 21:50 Comment(0)
G
6

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.

Gascony answered 5/8, 2014 at 22:2 Comment(2)
I've found this to be unreliable. Sometimes it stops monitoring all of the regions, other times it only stops monitoring some of them.Rora
Interesting, in that case, have you considered reusing the same identifiers? This will overwrite the regions that "are no longer useful"Gascony
E
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)
}
Editor answered 26/2, 2019 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.