6.2 · App and scene lifecycle in UIKit and SwiftUI

The most asked platform question at every level: what runs, in what order, and what the system may do to you between events.

Mental model. Since iOS 13 the app is about the process and the scene is about one window of UI. Most lifecycle bugs come from putting a scene’s job in the app’s methods, or the other way round.

States and callbacks

The five states and what runs on each transition.

  1. What are the app states, and which callbacks fire as you move between them? 6.2.1 · L1
    An app is always in one of five states. Not running: no process. Inactive: in the foreground but not receiving events (during a system alert, a call overlay…
  2. What is a scene, and what is the split between AppDelegate and SceneDelegate? 6.2.2 · L2
    A scene (UIScene) is one instance of your app’s UI: a window, or on iPad one of several windows, with its own lifecycle, state and (optionally) its own user…

Entry points

What launches your code, in UIKit and in SwiftUI.

  1. What happens inside UIApplicationMain, and how does @main on a SwiftUI App differ? 6.2.3 · L2
    Every UIKit app starts at a main function that calls UIApplicationMain (the @main attribute on the app delegate generates it). It creates the single…
  2. How does the SwiftUI lifecycle map onto UIKit’s, and what is scenePhase for? 6.2.4 · L2
    SwiftUI replaces per-callback methods with observable state. The app declares Scenes (usually a WindowGroup), and the environment value scenePhase reports…

Surviving termination

Saving and restoring what the user was doing.

  1. What does state restoration restore, and how do you make an app survive being killed in the background? 6.2.5 · L3
    The system may terminate a backgrounded app at any time to reclaim memory, and the user will still expect to return to what they were doing. State restoration…

More in iOS platform internals