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?
Can you stop regions from persisting between launches with CLLocationManager?
Asked Answered
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.
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
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)
}
© 2022 - 2024 — McMap. All rights reserved.