When moving a large UIKit app to the SwiftUI App life cycle, how do you plan the app delegate, scene delegate and window changes?
answer
- a new @main App struct
- the delegate survives via an adaptor
- storyboard keys come out
- scenePhase replaces life cycle callbacks
- multi-window once state is ready
basics
~20 sAdd an @main App struct, keep the old app delegate through @UIApplicationDelegateAdaptor, remove storyboard launch keys, move foreground and background handling to scenePhase, and host existing screens in a WindowGroup. Enable iPad multi-window only once per-window state is ready.
solid answer
~40 sThe entry point flips in one step, but the screens do not have to. I add a new `App` struct marked `@main` and remove `@main` or `@UIApplicationMain` from the old app delegate, which keeps working through `@UIApplicationDelegateAdaptor` for callbacks SwiftUI does not model, like the push token. I remove `Main.storyboard`, `UIMainStoryboardFile` and the scene configuration's `UISceneStoryboardFile`, stop setting a root view controller in `application(_:didFinishLaunchingWithOptions:)`, and remove `scene(_:willConnectTo:options:)` if it launched the UI; the scene delegate is still called for other events. Foreground and background handling moves to `scenePhase`. Existing UIKit screens are hosted inside the `WindowGroup` and converted later. The real judgment calls are which delegate callbacks stay, and when to enable multiple iPad windows: each window gets independent view state, so app-wide singletons must be ready for that first.
go deeper
Recall the first step: a new @main App struct with a WindowGroup, and the old app delegate loses its @main attribute.
Explain the mechanical steps: the delegate adaptor, removing storyboard keys, returning true from launch, and moving to scenePhase.
Show where regressions hide: launch from notifications, shortcuts and links, leftover window setup, and restoration with several windows.
Own the sequencing: flip the skeleton early, convert screens later, write a rule for the delegate, and gate multi-window on state ownership.
## What actually changes Moving to the SwiftUI life cycle replaces the app's skeleton, not its screens. Apple's migration guide describes it as changing the entry point, configuring launch, and monitoring life cycle changes with the methods SwiftUI provides. | Concern | UIKit life cycle | SwiftUI life cycle | |---|---|---| | Entry point | `@main` on the app delegate | `@main` on a type conforming to `App` | | Windows and root UI | Storyboard or code in a delegate | Scenes such as `WindowGroup` | | App delegate callbacks | The app delegate itself | A delegate kept through `@UIApplicationDelegateAdaptor` | | Foreground and background | Delegate callbacks | `scenePhase` from the environment | | Scene-level events | Scene delegate from Info.plist | Scene delegate returned from the app delegate's configuration, or still called after the storyboard keys go | ## A staged plan 1. **Entry point.** Create `<AppName>App.swift` with an `App` struct marked `@main` and a `WindowGroup`. Remove `@main` or `@UIApplicationMain` from the app delegate, because an app has exactly one entry point. 2. **Keep the delegate.** Declare `@UIApplicationDelegateAdaptor` once in the `App` so existing delegate methods, such as remote-notification registration, keep running. SwiftUI now creates that delegate. 3. **Launch configuration.** Remove `Main.storyboard`, the `UIMainStoryboardFile` key, and `UISceneStoryboardFile` from the default scene configuration in the scene manifest. If the app delegate set a root view controller in `application(_:didFinishLaunchingWithOptions:)`, stop doing so and return `true`. 4. **Scene delegate.** Apple's guide notes that the scene delegate continues to be called after the storyboard keys are removed, so other scene events keep working; if the app was launched from the scene delegate, remove its `scene(_:willConnectTo:options:)` method. A scene delegate can also be supplied from the app delegate's scene configuration. 5. **Life cycle monitoring.** Move foreground and background handling to `scenePhase`, reading it in views for per-window work and in the `App` for app-wide work. 6. **Screens.** Host the existing root view controller inside the `WindowGroup` through the UIKit bridging types, and convert screens to SwiftUI on the product's own schedule. ## Decisions a lead owns - **Big bang or incremental.** The entry point is all or nothing, but screens are not; most teams flip the skeleton in one release and keep UIKit screens hosted for months. The cost is a period with both models in the codebase. - **What stays in the delegate.** Push token, notification and system callbacks with no SwiftUI equivalent stay; life cycle reactions, window creation and shared state move out. Writing that rule down stops the delegate from becoming a second life cycle. - **When to enable multiple windows.** On iPad, setting `UIApplicationSupportsMultipleScenes` to `true` lets people open several windows from one `WindowGroup`, and each gets independent view state. Singletons that assume one screen, a "current document" global for example, break first. Enable it after shared models are owned by the `App` and per-window state lives in each window's views. - **Mac and iPad commands.** Once scenes exist, menu bar commands come from the `commands(content:)` scene modifier; decide which existing keyboard shortcuts move there. - **How to roll it out.** `@main` is fixed at compile time, so the new entry point cannot sit behind a runtime flag; the switch ships as a whole release. Mitigate with a longer beta, a gradual release, and a small first release that changes only the skeleton, so any launch regression is clearly attributable to it. ## Risks and how to test them - **Launch paths.** Cold launch from a notification, from an app shortcut, and from a link each used to pass through delegate code; walk every one after the switch. - **Duplicate adaptors.** Declaring the adaptor in more than one place is a runtime error; keep it in the `App` only. - **Leftover window setup.** A root view controller assigned in the launch callback competes with the scene's window. - **Restoration.** Per-window values and scene storage behave differently from a hand-rolled restoration scheme; test relaunch with several windows open. ## When to wait - The app relies heavily on custom window management, or on setup in `scene(_:willConnectTo:options:)` that has no SwiftUI equivalent the team has mapped yet. - The team cannot yet test every launch path, which is where regressions in this migration hide. - Multi-window is a product goal but app state is still global; fix ownership first, then flip.
- What happens to an existing UIWindowSceneDelegate after the app switches to the SwiftUI App life cycle?Apple's migration guide says the scene delegate continues to be called after the storyboard keys are removed, so other scene-based events keep working. If the app used to launch its UI there, remove the scene(_:willConnectTo:options:) method, because scenes declared in the App now create the windows. A scene delegate can also be supplied from the app delegate's application(_:configurationForConnecting:options:).
- Why might you delay setting UIApplicationSupportsMultipleScenes to true during this migration?Because each WindowGroup window gets independent view state, while legacy code often assumes one screen through singletons such as a current-document global. Two windows would then fight over that global. Move shared models to the App, keep per-window choices in each window's views, and only then let people open several windows.
- Keep @main on the AppDelegate and add a second @main App struct beside it.
- The migration requires rewriting every UIKit screen in SwiftUI before it ships.
- Keep creating the window and root view controller in the launch callback.
- After the switch, the scene delegate stops receiving any callbacks.
- Turning on multiple scenes costs nothing once the App struct exists.