skip to content

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

level: middleimportance: should knowfreq 30%

answer

  1. four scene types, four jobs
  2. template versus one unique window
  3. Settings menu item, Mac only
  4. document browser on iOS

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.

solid answer

~40 s

All four are `Scene` types you list in the `App` body. `WindowGroup` treats its content as a template and creates as many independent windows as the platform allows. `Window` declares one unique window that `openWindow(id:)` brings to the front instead of duplicating; the SDK makes it available on macOS 13 and visionOS 26 and unavailable on iOS. `Settings` is macOS-only: it enables the app's Settings menu item, so a multiplatform app wraps it in `#if os(macOS)`. `DocumentGroup` adds document support: a document browser and multiple windows on iOS, document menus on macOS. The iOS 27 SDK gives it a class-based `Document` protocol with new initializers and marks `FileDocument` for deprecation in a future release. On iPhone and iPad the real choice is between `WindowGroup` and `DocumentGroup`.

code

swift · 28 lines
swift
import SwiftUI

@main
struct EditorApp: App {
    var body: some Scene {
        WindowGroup {
            LibraryView()
        }
        .commands {
            CommandMenu("Document") {
                Button("Export as PDF") { }
                    .keyboardShortcut("e", modifiers: [.command, .shift])
            }
        }
        #if os(macOS)
        Settings {
            SettingsView()
        }
        Window("Activity", id: "activity") {
            ActivityView()
        }
        #endif
    }
}

struct LibraryView: View { var body: some View { Text("Library") } }
struct SettingsView: View { var body: some View { Text("Settings") } }
struct ActivityView: View { var body: some View { Text("Activity") } }

go deeper

for a junior

Recall that WindowGroup is the default scene and that the others exist for single windows, settings and documents.

for a middle

Explain what each scene type promises and which ones the iOS SDK marks unavailable, and why #if os(macOS) is needed.

for a senior

Show judgment on DocumentGroup: content-type ordering, Info.plist declarations, and what the iOS 27 Document protocol changes for new code.

for a principal

Decide the scene structure for a multiplatform product: which scenes each platform gets, and how menus and settings map across iPad and Mac.

## Scenes are the building blocks of an App An app that uses the SwiftUI life cycle lists its **scenes** in the `body` of its `App` type. A scene is a part of the interface whose life cycle the system manages, and each built-in scene type encodes a different promise about windows. Choosing the wrong one either fails to compile on a platform or gives people a window model they do not expect. ## The four scene types side by side | Scene | What it creates | iOS | macOS | |---|---|---|---| | `WindowGroup` | Any number of identically structured, independent windows | Yes, since iOS 14 | Yes | | `Window` | One unique window, identified by an id | Unavailable | Yes, since macOS 13 | | `Settings` | The window behind the app's Settings menu item | Unavailable | Yes | | `DocumentGroup` | Windows that open, create and save documents | Yes, since iOS 14 | Yes | The availability column comes straight from the `@available` lines in the iOS 27 SDK: `Window` is declared for macOS 13 and visionOS 26 with `iOS, unavailable`, and `Settings` is unavailable on iOS, tvOS, watchOS and visionOS. ## WindowGroup - The view hierarchy you pass is a **template**; each window is a new instance with its own `@State`. - On iPhone there is one window. On iPadOS, people can open several only when the Info.plist scene manifest sets `UIApplicationSupportsMultipleScenes` to `true`. On macOS, windows can be gathered into tabs and get standard window commands. - Initializing it with an `id`, a presented value type, or both lets code open new windows with the `openWindow` environment action. ## Window and Settings: Mac-first scenes - **`Window`** is for something there should only ever be one of, such as an activity monitor. Calling `openWindow(id:)` with its id orders the existing window to the front rather than creating another. - **`Settings`** hands SwiftUI a view for app preferences. Declaring it enables the app's Settings menu item, and SwiftUI shows and removes the view when people choose that item or press its keyboard shortcut. Its controls typically bind to `@AppStorage` values. - Because both are unavailable on iOS, a multiplatform `App` must wrap them in `#if os(macOS)`; otherwise the iOS build fails to compile. On iPhone and iPad, settings usually live in a screen of the main window instead. ## DocumentGroup and the iOS 27 document model `DocumentGroup` tells SwiftUI which kinds of documents the app can open. On iOS that brings a **document browser** for the file system and multiwindow support; on macOS it brings document menus and multiple open documents. Some details interviewers probe: - An app can declare several document groups. SwiftUI checks them **top to bottom** and uses the first whose document type's readable content types include the file's type, so the more specific group must come first. - The app's Info.plist still has to declare its document types and any custom content types it exports or imports. - Before the iOS 27 SDK, documents conform to `FileDocument` (a value type) or `ReferenceFileDocument` (a class). The **iOS 27 SDK** adds a class-based `Document` protocol, built from `ReadableDocument` and `WritableDocument`, plus initializers such as `DocumentGroup(allowCreating:editor:makeDocument:)` that expect an `@Observable` document. It also marks `FileDocument` and `ReferenceFileDocument` for deprecation in a future release, so new iOS 27 code should look at the new protocol while apps deploying to earlier releases keep the older ones. ## Menus and commands across scenes Scenes also carry the menu bar. The `commands(content:)` scene modifier, available on iOS 14 and macOS 11, adds items to a macOS or iPadOS menu bar: 1. `CommandMenu("Name") { ... }` adds a new top-level menu. 2. `CommandGroup(after:)`, `CommandGroup(before:)` and `CommandGroup(replacing:)` place items relative to standard groups such as `.newItem` or `.saveItem`. 3. A button inside can take `keyboardShortcut(_:modifiers:)` so the command also works from a hardware keyboard. ## Choosing 1. An iPhone-first app with one kind of content: a single `WindowGroup`. 2. An iPad and Mac app where people want several views of their data: a `WindowGroup`, possibly a second one opened with values. 3. An app whose content is files people own: `DocumentGroup`, which brings browsing, opening, saving and file handling. 4. Mac-only extras: `Window` for singletons and `Settings` for preferences, both behind `#if os(macOS)`.

  • How do you add a custom menu to the menu bar of a SwiftUI app on iPad and Mac?
    Apply the commands(content:) scene modifier, usually to the main WindowGroup, and build it from Commands values: CommandMenu adds a new top-level menu, and CommandGroup inserts items before or after a standard placement such as .newItem, or replaces it. Buttons can carry keyboardShortcut so the command also works from a hardware keyboard.
  • If a SwiftUI app declares two DocumentGroup scenes whose types could both open a file, which one is used?
    SwiftUI checks the document groups from top to bottom and uses the first whose document type lists a readable content type the file conforms to. Content types form a hierarchy, so a plain-text group also matches HTML; declare the more specific group first.

  • Window is just a fixed-size WindowGroup and behaves the same on iPhone.
  • Declaring a Settings scene adds a settings screen to an iOS app.
  • An iPad app shows multiple WindowGroup windows without any Info.plist change.
  • With DocumentGroup you still have to build your own document browser on iOS.
  • FileDocument is the new document model introduced in the iOS 27 SDK.

Assumes the iOS 27 SDK, which adds the class-based Document protocol and new DocumentGroup initializers and marks FileDocument for future deprecation; the iOS availability of the four scene types is unchanged since iOS 14.