Application Services

The state of an application is more important on the iPhone than it is on the desktop. This is because many events on the iPhone can cause an application to suspend, run in the background, or terminate. These different states occur when the user presses the home button, locks the screen, or receives a phone call. It's important for an application to know when its state is about to change to save any settings, halt threads, or perform other actions.

The UIApplication base class contains many functions for application state changes, which can be overridden by the application. While there's nothing the application can generally do about the state it's about to enter, it can at least take whatever actions are appropriate to prepare for it.

Suspending

When the home button is pressed, the default behavior for an application is to suspend. It's also told to suspend during a phone call or when the screen is locked. When this happens, the application's suspend methods are called.

Depending on the nature of the event, three different suspend methods might be called.

applicationWillSuspendUnderLock

The iPhone screen is locked either by pressing the power button or during an idle screen lock.

applicationWillSuspendForEventsOnly

Events that force the application into the background, such as receiving a phone call, cause this method to be called. Also, if the display is locked, but no applicationWillSuspendUnderLock method is overridden, this method gets called instead.

applicationSuspend ...

Get iPhone Open Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.