Home / iPad


Apps are generally moved to the background when interrupted or when the user quits. But if the app was compiled with an earlier version of the SDK or is running on an earlier version of the operating system that doesn't support multitasking - or you decide you don't want your app to run in the background and you set the UIApplicationExitsOnSuspend key in its Info. plist file - the Terminator stomps on your app.

What happens is this: Your app delegate is sent the applicationWill Terminate: message, and you have the opportunity to add code to do whatever you want to do before termination, including saving the state the user was in. Saving is important, because then, when the app is launched again (refer to Step 2 in "App Anatomy 101," earlier in this tutorial) and the UIApplicationMain sends the app delegate the applicationDidFinishLaunching message, you can restore the app to the state the user left it (such as a certain view). This is the same thing you would have done in the application method applicationDidEnter Background: - cover this in "Responding to interruptions" in this tutorial.

Tip It's worth noting that before the application is terminated and the applicationWillTerminate: message is sent, the applicationDid EnterBackground: message is also sent. This means that for applications you compile and run only under iOS 4.2 and beyond, you only have to do all that cleanup and file saving in applicationDidEnterBackground:.

Your applicationWillTerminate: method implementation has about five seconds to do what it needs to do and return. Any longer than that and your application is terminated and purged from memory. (The Terminator doesn't kid around.)

Remember Even if you develop your application using the iOS 4.2 SDK and newer versions - as you will be doing, if you stay as up-to-date as me - you must still be prepared for your application to be terminated. If memory becomes an issue (and it inevitably will if there are enough apps in the background), the system might remove your application from memory in order to make more room. If it does remove your suspended application, it does not give you any warning, much less notice! However, if your application is currently running in the background, the system does call the applicationWillTerminate: method of the application delegate.

[Previous] [Contents] [Next]