UISceneDelegate: Managing Your App's UI Instances

Think of SceneDelegate as the lifecycle manager for a single window of your app's UI. It's essential for multi-window iPad apps, handling when a scene connects, disconnects, or changes state.
WHY IT EXISTS: Before iOS 13, the AppDelegate managed both the app's process lifecycle and its single UI window. To support multiple windows on iPadOS and macOS, Apple needed to separate the concept of the app's process from the lifecycle of its UI. UISceneDelegate was created to manage a single UI instance, or "scene," allowing an app to have several running concurrently.
THE MENTAL MODEL: Think of AppDelegate as the theater owner, responsible for opening and closing the entire building (the app process). UISceneDelegate is the stage manager for one specific play (a UIWindowScene) within that theater. The stage manager handles when the curtain rises (sceneWillEnterForeground), when the lights dim for intermission (sceneDidEnterBackground), and when that specific play is over for the night (sceneDidDisconnect). An app can have multiple plays running at once, each with its own stage manager.
HOW IT WORKS: When your app needs to show a new UI instance, the system creates a UISceneSession object. For each session, UIKit creates a corresponding UIScene (like a UIWindowScene) and attaches your custom UISceneDelegate to it. Your delegate then receives callbacks for lifecycle events. The most critical method is scene(_:willConnectTo:options:), where you are given the scene object. Here, you must create a UIWindow, assign it a rootViewController, and make it visible. Other methods like sceneDidBecomeActive, sceneWillResignActive, and sceneDidEnterBackground allow you to respond to focus and visibility changes for that specific scene.
WHEN TO USE IT: You use UISceneDelegate in any iOS 13+ app that adopts the scene-based lifecycle, which is the default for all new projects. It is the correct place to configure your UI windows and respond to UI-specific state changes, especially if you want to support features like multiple windows on iPadOS or Split View.
WHEN NOT TO USE IT: If you are maintaining a legacy application that has explicitly opted out of the scene-based lifecycle (by removing the UIApplicationSceneManifest key from its Info.plist), you would continue to manage the UI window from the AppDelegate. For any new development, however, using the scene-based approach with UISceneDelegate is standard practice.
ONE CANONICAL EXAMPLE: A typical implementation of the primary setup method, scene(_:willConnectTo:options:), involves a few key steps. First, you safely unwrap the scene as a UIWindowScene: guard let windowScene = (scene as? UIWindowScene) else { return }. Next, you initialize a UIWindow with that scene: let window = UIWindow(windowScene: windowScene). Then, you set its content: window.rootViewController = MainViewController(). Finally, you store a reference to the window and make it visible: self.window = window; window.makeKeyAndVisible().
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.