Article

Managing Your App's Life Cycle

Understand how the app delegate manages your app's high-level behavior.

Overview

UIKit apps are always in one of five states, which are shown in Figure 1. Apps start off not running. When the user explicitly launches the app, the app moves briefly to the inactive state before entering the active state. (An active app appears onscreen and is known as a foreground app.) Quitting an active app moves it offscreen and into the background state, where it stays until the system suspends it a short time later. At its discretion, the system may quietly terminate a suspended app, returning it to the not running state.

Figure 1

The execution states of a UIKit app

Apps start in the not running state. They progress to the inactive and active state when launched, or to the background state when running in the background. An app is suspended when it is not frontmost and may eventually return to the not running state.

Your app’s current state defines what system resources are available to it. Because active apps are visible onscreen and must respond to user interactions, they have priority when it comes to using system resources. Background apps are not visible onscreen, and therefore have more limited access to system resources and receive limited execution time.

Manage Life Cycle Events

When your app transitions from one state to another, UIKit notifies your app delegate object—an object that conforms to the UIApplicationDelegate protocol. Use your app delegate to modify your app's behavior to match its new state. For example, when moving from the not running to inactive state, handle the launch-time tasks needed to prepare your app to run.

The system notifies your app delegate about the following transitions:

Manage Behavioral Events

Your app delegate also responds to some other important events:

  • Memory warnings. Reduce the amount of memory your app uses; see Responding to Memory Warnings.

  • Time changes. Update time-sensitive features of your app.

  • Protected data becomes available/unavailable. Manage files when the user locks or unlocks the device.

  • State restoration. Restore your app’s UI to its previous state, giving the appearance that your app never stopped running; see Preserving Your App's UI Across Launches.

  • Handoff tasks. Continue tasks started on another device.

  • Open URLs. Receive and open URLs sent to your app.

  • Inter-app communication. Receive data from a paired watchOS app.

  • File downloads. Receive files that your app downloaded using a URLSession object.

Although the app delegate is the primary place where you handle life cycle events, it is not the only place. For most events, UIKit also generates notifications that any object can observe. For a list of app-related notifications that you can observe, see UIApplication. For more information about the methods you use to handle events, see UIApplicationDelegate.

Topics

Life Cycle Events

Responding to the Launch of Your App

Initialize your app and prepare it to run.

Preparing Your App to Run in the Foreground

Configure your app to appear onscreen.

Preparing Your App to Run in the Background

Prepare your app to be suspended.

Behavioral Events

Responding to Memory Warnings

Free up memory when asked to do so by the system.

Preserving Your App's UI Across Launches

Return your app to its previous state after it is terminated by the system.

See Also

Application

class UIApplication

The centralized point of control and coordination for apps running in iOS.

protocol UIApplicationDelegate

A set of methods that are called by the singleton UIApplication object in response to important events in the lifetime of your app.

Allowing Apps and Websites to Link to Your Content

Use universal links to link to content within your app and share data securely.

func UIApplicationMain(Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>, String?, String?) -> Int32

Creates the application object and the application delegate and sets up the event cycle.