iOS App Development: Understand State Transitions

The UIApplication object provides the application-wide control and coordination for an iOS application. It is responsible for handling the initial routing of incoming user events (touches, for example) as well as dispatching action messages from control objects (such as buttons) to the appropriate target objects.

The application object sends messages to its Application Delegate to allow you to respond, in an application-unique way, when your application is executing, to things such as application launch, low-memory warnings, and state transitions, such as moving into background and back into foreground.

You should implement the following UIAppDelegate methods in your application. Most of these methods are already basically implemented in the code provided by Apple’s templates, complete with comments explaining their purposes.

Method

What You Do with It

application:didFinishLaunchingWithOptions:

In this method, do what you need to do to initialize your
application after it’s launched.

applicationWillResignActive:

This message is sent when the application is about to move from
the active to inactive state. Use this method to do things such as
pause ongoing tasks and anything based on a timer (such as a game).
Using this method doesn’t mean that you will be entering
background, but it does mean that your application won’t be
executing.

applicationDidEnterBackground:

This message is sent when your application is going to be
entering background. At this point, you need to assume that your
application may eventually be terminated without warning, so save
user data, invalidate timers, and store enough application state
information.

applicationWillEnterForeground:

This message is sent when your application has been rescued
from background. In this method, reverse what you did in applicationDidEnterBackground:.

applicationDidBecomeActive:

Your application is now active. You should reverse whatever you
did in applicationWillResignActive:. You
also might want to refresh the user interface.