skip to content

Apps, Scenes and Windows

An app declares scenes such as WindowGroup and Settings, reacts to scene phase changes and adapts app delegates. Interviewers ask where launch work goes and how multiple windows behave.

on this pageshow

explore

questions

6

In SwiftUI, what does an @main App type declare, and how does a WindowGroup turn its content into windows?

level: juniorimportance: must knowfreq 72%

answer

  1. one entry point per app
  2. body returns scenes, not views
  3. WindowGroup content is a template
  4. fresh @State storage per window

basics

~20 s

A type conforming to App and marked @main is the app's single entry point, and its body returns scenes. A WindowGroup's content is a template: every window it creates gets its own view hierarchy and its own @State storage.

solid answer

~40 s

The `App` protocol describes the whole app: you mark one conforming struct with `@main`, and the protocol's default `main()` launches it. Its `body` is a scene builder that returns `Scene` values, not views. `WindowGroup` is the everyday scene: the view hierarchy you give it is a template, and each window SwiftUI creates from it is an independent instance, so `@State` and `@StateObject` storage is allocated per window. On iPhone that means one full-screen window; on iPadOS (with `UIApplicationSupportsMultipleScenes` set to `true`) and on macOS, people can open several. State that every window must share, such as a document store, belongs in the `App` itself and is handed down through the environment or an initializer.

code

swift · 28 lines
swift
import Observation
import SwiftUI

@Observable
final class DocumentStore {
    var titles: [String] = []
}

@main
struct EditorApp: App {
    @State private var store = DocumentStore()   // one per app

    var body: some Scene {
        WindowGroup {
            EditorRoot()
                .environment(store)
        }
    }
}

struct EditorRoot: View {
    @Environment(DocumentStore.self) private var store
    @State private var selectedIndex = 0         // one per window

    var body: some View {
        Text(store.titles.isEmpty ? "No documents" : store.titles[selectedIndex])
    }
}

go deeper

for a junior

Recall the shape: one @main type conforming to App, a body that returns scenes, and a WindowGroup wrapping the first view.

for a middle

Explain that a WindowGroup is a template and that each window gets new @State storage, then say where shared models must live.

for a senior

Show you can place state deliberately: app-owned models injected through the environment, per-window selection in view state, launch work outside views.

for a principal

Frame the App struct as the composition root: what a team allows there, how shared services are injected, and how that choice scales to multiple windows.

## What the App type is Every app built with the SwiftUI life cycle starts from one type that conforms to the **`App` protocol**. The protocol has two requirements: an `init()` and a `body` property whose type conforms to `Scene`. You put the **`@main` attribute** on that type to say "this is where the program starts", and the protocol supplies a default `main()` that the system calls to launch the app. Apple's documentation is explicit that you can have **exactly one entry point** among all of the app's files, so a second `@main` type is a compile error, not a second app. `App` is declared `@MainActor`, and a type that adopts it in its own declaration inherits that main-actor isolation. ## Scenes, not views The `body` of an `App` is marked with `@SceneBuilder` and returns **scenes**. A scene is a part of the user interface whose life cycle the system manages; it acts as the container for a view hierarchy. The system decides how a scene appears: it might fill the whole display, become a resizable window, or a tab in a window, depending on the platform and the scene type. That is why the body returns `some Scene` and why the first view you write sits *inside* a scene rather than at the top level. - `WindowGroup` is the scene almost every iOS app declares. - Other scene types exist for documents and, on the Mac, for single windows and settings; those are a separate topic. - Scenes take their own modifiers, such as `commands(content:)` or `onChange(of:initial:_:)`, the same way views take view modifiers. You can also write a **custom scene**: a type that conforms to `Scene` and whose own `body` returns other scenes, for example a `WindowGroup` together with its modifiers. That keeps a large `App` body readable, and it is the same composition idea as building a view from smaller views. The `App` body then lists the custom scene as if it were a built-in one. ## WindowGroup is a template A `WindowGroup` does not hold one window. The view hierarchy you pass it is a **template**, and every window the app creates from the group is a fresh instance of that template. Apple's documentation states the consequence directly: every window in the group maintains independent state, and the system allocates new storage for any `State` or `StateObject` variables in the scene's view hierarchy for each window it creates. | Platform | What the group produces | |---|---| | iPhone | One window that fills the display | | iPadOS | Several windows, but only when the Info.plist scene manifest sets `UIApplicationSupportsMultipleScenes` to `true` | | macOS | Several windows, which people can gather into tabs; standard window commands come for free | ## Where shared state lives Because every window owns its own copy of view state, anything that all windows must agree on has to live *above* the windows. The usual place is the `App` struct: 1. Create the shared model once in the app, for example `@State private var store = DocumentStore()` for an `@Observable` class, or `@StateObject` for an `ObservableObject`. 2. Pass it into the group's content through the environment, or through an initializer. 3. Keep per-window choices, such as which document a window is showing or which tab is selected, as `@State` in the window's views. Getting this split wrong produces two classic bugs: a "shared" model created in a root view turns into one model per window, and a "per-window" selection stored in the app makes every window jump together. ## Launch work A view's appearance hooks run once per window and again whenever the view reappears, so they are the wrong place for one-time setup. App-wide setup belongs with the app: in the `App` initializer, which runs when `main()` creates the app at launch, or in an app delegate adaptor when it needs a UIKit delegate callback. ## Mistakes interviewers listen for - Saying the `App` body returns the root view. It returns scenes; the root view is the content of a scene. - Assuming two windows from one group share `@State`. They do not. - Expecting multiple iPad windows without setting the scene-manifest key. - Creating a shared store inside the group's root view and wondering why each window has different data. - Putting `@main` on something other than the `App` conformer, or on two types at once.

  • Where does one-time launch work, such as configuring a crash reporter, go in a SwiftUI app?
    Not in a view's onAppear or task, because those run per window and again when a view reappears. Put it in the App's initializer, which runs when main() creates the app at launch, or in an app delegate adaptor's launch callback when the work needs a UIKit delegate method. Keep it short, since it sits on the launch path.
  • What happens to a view's @State when a person opens a second iPad window from the same WindowGroup?
    Nothing is shared. SwiftUI allocates new storage for every @State and @StateObject in the hierarchy of each new window, so the second window starts from initial values. Data both windows must see belongs in a model owned above the windows, typically in the App, and passed down through the environment.

  • The @main App struct's body returns the root view directly.
  • A WindowGroup always creates exactly one window on every platform.
  • Two windows from the same WindowGroup share their @State values.
  • Several types in one target can each be marked @main as alternative entry points.
  • Creating the shared store in the root view's onAppear is fine because it runs once.
open as a page

In SwiftUI, how does the scenePhase environment value differ when read in a view versus in the App, and where do you save edits?

level: middleimportance: must knowfreq 65%

basics

~20 s

Read in a view, scenePhase is the phase of the scene containing that view; read in the App, it aggregates every scene. Save a window's edits from its own view when that scene's phase changes; free app-wide resources in the App.

open as a page

In SwiftUI, how do WindowGroup, Window, Settings and DocumentGroup differ, and which of them can an iOS app declare?

level: middleimportance: should knowfreq 30%

basics

~20 s

WindowGroup opens identical, independent windows; Window is one unique window; Settings drives the Mac Settings menu item; DocumentGroup adds document browsing, opening and saving. Of the four, only WindowGroup and DocumentGroup are available on iOS.

open as a page

In a SwiftUI app, how do you receive the push-notification device token through @UIApplicationDelegateAdaptor, and what should stay out of that delegate?

level: seniorimportance: should knowfreq 50%

basics

~10 s

Write an NSObject class conforming to UIApplicationDelegate that implements application(_:didRegisterForRemoteNotificationsWithDeviceToken:), and declare it once in the App with @UIApplicationDelegateAdaptor. SwiftUI creates it and forwards callbacks; foreground and background handling stays with scenePhase.

open as a page

When moving a large UIKit app to the SwiftUI App life cycle, how do you plan the app delegate, scene delegate and window changes?

level: principalimportance: should knowfreq 25%

basics

~20 s

Add 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.

open as a page

A SwiftUI document editor calls openWindow(value: document.id) to open a document in its own iPad window, but nothing appears; what do you check?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

Check that windows are possible: supportsMultipleWindows is false on iPhone and on iPad without UIApplicationSupportsMultipleScenes, and SwiftUI then ignores openWindow. Then check that a WindowGroup(for:) declares the exact value type, and that no window already shows that value.

open as a page