What is a scene, and what is the split between AppDelegate and SceneDelegate?
Predict first
Which delegate receives did become active for a window in a scene-based app?
// A: the app delegate
// B: the scene delegate
// C: the view controller only
- A
- B
- C
Mental model. The app is the building and the scenes are the rooms. Building-wide events go to the manager (app delegate), and events in a room go to that room’s host (scene delegate).
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 activity. The system creates it and tracks it as a UISceneSession, which can outlive the scene: the system may discard the scene’s UI when it goes to the background (sceneDidDisconnect) and later recreate it from the session. The app can therefore have several scenes at once, and any of them can be created or destroyed without the process ending.
The split follows from that. The app delegate owns process-wide events: didFinishLaunching, remote-notification registration, background URLSession events, memory warnings, and choosing which scene configuration to use. The scene delegate owns per-window events: creating the window and root view controller (scene(_:willConnectTo:options:)), becoming active or inactive, entering the background, opening URLs, and handing off activities. Scenes are declared in the UIApplicationSceneManifest of Info.plist. Per-window state belongs to the scene, never to a singleton or the app delegate.
UI lifecycle events are per scene, so they go to the scene delegate. The app delegate keeps process-level events.
Common traps
- Keeping the window or current screen in the app delegate. With multiple scenes there is more than one.
- Expecting
applicationDidEnterBackgroundin a scene-based app. The scene methods replace it for UI events.
They might follow up with
- What is a scene session for? It is the persistent record of a scene, used to restore its UI after the system discards it.
- Do iPhone apps need scenes? They use the same lifecycle; a single scene is the common case, and it is the required model in recent SDKs.
Related questions
- 6.2.1 What are the app states, and which callbacks fire as you move between them?
- 6.2.5 What does state restoration restore, and how do you make an app survive being killed in the background?
- 6.2.3 What happens inside UIApplicationMain, and how does @main on a SwiftUI App differ?
← What are the app states, and which callbacks fire as you move between… What happens inside UIApplicationMain, and how does @main on a… →