Thursday, July 14, 2011

How to Prevent Your Application from Being Placed into the Background

By default, when running on a device that supports multitasking, if the user quits the application (pressing the Home button), the application will be notified and moved to the background. This is nice as the response time when the user requests the application again is generally very quick, not to mention the application resumes in the same place/state as when it exited.
Even with this built-in convenience, they are times when this default behavior may not be ideal. For example, depending on the application, it may require a fair amount of additional development time to properly manage this state change. In such cases, simply quiting the application may be preferred.


UIApplicationExitsOnSuspend
To request the application exit rather than be suspended, add the key UIApplicationExitsOnSuspend to the application’s plist file. The figure below shows how this looks if you edit the plist as an XML file.
<plist version="1.0">
<dict>
...
<key>UIApplicationExitsOnSuspend</key>
<true/>
...
</dict>
</plist>
If you prefer to edit the file using the Property List editor, the change would look as follows:

Application Shutdown
If you’ve added the plist entry above, just as on non-multitasking devices, when the user taps the Home key the method applicationWillTerminate: in the application delegate will be called. You have somewhere in the neighborhood of 5 seconds to save any pertinent information before the application is terminated.
Although there are times to use this approach, it is generally recommended for the best user experience to design/develop your applications to manage being placed into the background.

No comments:

Post a Comment