Search code examples
ioscllocationmanagercllocation

how to stop location manager when UIViewController disappear


I have start location manager in -viewDidLoad: method. Similarly, I need to stop it when user move to another view. how do I do that?

I tried to stop it in -viewDidUnload, but it didn't work because I think it only gets called when memory is low.


Solution

  • You can use -viewWillAppear: and -viewWillDisappear: to start and stop locating user, instead of -viewDidLoad / -viewDidUnload. There might be better ways to do it; that's the way I did it in one of my own apps.

    Alternatively, you can register for UIApplication notifications, e.g. UIApplicationWillResignActiveNotification, to stop updating location, in case user pressed the home button. I think that's the kind of behavior Maps.app implements.