Stop CLLocationManager when user stops app using Fast App Switcher

375 views Asked by At

I am developing an iOS Application which uses CLLocationManager to get GPS Data.

What's a proper way to stop CLLocationManager when the user presses the home button and then kills the app using fast app switcher?

How can I stop the CLLocationManager then to stop polling for GPS data? Does anybody knows a good way to achieve this, cause normally I can't execute any code when the user kills the application...

EDIT: When I am sending the application to background, it should still get significantLocationChanges, so I can't stop CLLocationManager when the application is sent to the background!

1

There are 1 answers

4
SteveB On BEST ANSWER

In the AppDelegate there is a method called applicationWillResignActive. This method runs when your app is about to be sent to the background. If you have access to the CLLocationManager from your AppDelegate you can stop it right there. Otherwise you can post a Notification from applicationWillResignActive and whatever class has your CLLocationManager can subscribe to that Notification and handle it from there.

Post a Notification like this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appWillResignActive" object:nil];

And subscribe like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleResign:) name:@"appWillResignActive" object:nil];

Then some method like this:

- (void)handleResign: (NSNotification *)notification
{
    // stop CLLocationManager
}

EDIT: If you want to run code when the app is being killed then you can run that code in this method in the AppDelegate:

- (void)applicationWillTerminate:(UIApplication *)application