1

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!

Jasperan
  • 2,154
  • 1
  • 16
  • 40
joshi737
  • 869
  • 3
  • 12
  • 26

1 Answers1

1

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
SteveB
  • 652
  • 3
  • 12
  • 1
    You dont need to post your own notifications if classes other than the app delegate need to respond to these events: see UIApplicationWillResignActiveNotification et al. – rickster May 05 '12 at 20:30
  • SteveB, rickster - I just edited my post, because I also need the Location changes while the application is in background, so I cant stop CLLocationManager when entering background. Anyway Thank you for your answers! – joshi737 May 06 '12 at 07:09
  • So you only want to stop when the app is manually killed by the user? When that happens all code in your app should cease to run. That's the point of killing it like that. At that point how can you even confirm that something is still happening? Are you outputting log files and seeing activity after the app is killed? – SteveB May 06 '12 at 14:10
  • right, I just want to stop CLLocationManager when the app is killed by the user. Thats just my question: How can I execute code when the app is killed by the fast app switcher to stop CLLocationManager or how do other apps stop CLLocationManager, especially those who are using background updates...??? Do you have any idea??? – joshi737 May 06 '12 at 16:24