Search code examples
iphoneobjective-csavensuserdefaultsbackgrounding

How to tell if iPhone application was launched by backgrounding or not


My application uses NSUserDefaults to store some values so that it can restore them on application update or if backgrounding is quit. Backgrounding automatically saves my integer values, but if the user quits the application from the launcher, the numbers are lost too, and the ViewDidUnload method I guess doesn't evoke when entering backgrounding. Is there a way that I can save the NSUserDefaults any time the application unloads. Also, the ViewDidLoad method had the same problem, it doesn't evoke from backgrounding. What's a way around this?

P.S. So far the only data my application needs to save is an int for an on-screen count.

UPDATE: UIApplicationDidEnterBackgroundingNotification works great with your suggestions!


Solution

  • You can't save data right before it eventually gets killed, but you can save state 'just in case' in your applicationDidEnterBackground: app delegate.

    There's also applicationDidBecomeActive:, but there's no reason to load your save data from there, since when it's invoked from an app that was in the background, the data will have been preserved any way.