skip to content

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%

answer

  1. platform support before the API
  2. multiple-scenes key in the plist
  3. a WindowGroup for that value type
  4. an existing window comes forward

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.

solid answer

~40 s

I work through it in order. First, can this device open windows at all? `@Environment(\.supportsMultipleWindows)` is `true` on iPadOS only when the app uses the SwiftUI life cycle and sets `UIApplicationSupportsMultipleScenes` to `true` in its scene manifest; when it is `false`, SwiftUI ignores the action and logs a runtime error. Second, does a scene accept the value? `openWindow(value:)` looks for a `WindowGroup(for:)` whose presented type matches, and that type must be `Hashable` and `Codable`; a value of another type matches no group. Third, is a window already showing that value? Then the system brings it to the front instead of opening a new one. Finally I make the window content cope with a `nil` binding, or give the group a `defaultValue`, because File > New Window and failed restoration arrive without a value.

code

swift · 26 lines
swift
import SwiftUI

@main
struct EditorApp: App {
    var body: some Scene {
        WindowGroup {
            Text("Library")
        }
        WindowGroup("Document", for: UUID.self) { $documentID in
            Text(documentID?.uuidString ?? "No document selected")
        }
    }
}

struct OpenInNewWindowButton: View {
    let documentID: UUID
    @Environment(\.openWindow) private var openWindow
    @Environment(\.supportsMultipleWindows) private var supportsMultipleWindows

    var body: some View {
        Button("Open in New Window") {
            openWindow(value: documentID)
        }
        .disabled(!supportsMultipleWindows)
    }
}

go deeper

for a junior

Recall that openWindow comes from the environment and that a WindowGroup must exist for the value you pass.

for a middle

Explain how SwiftUI matches a value to a WindowGroup by type, and why the type must be Hashable and Codable.

for a senior

Diagnose in order: supportsMultipleWindows and the scene-manifest key, the type match, an already-open window, then nil and restoration paths.

for a principal

Decide what deserves its own window on iPad and Mac, which values identify windows, and how restoration of those values is tested.

## The scenario A document editor lists documents in its main window. On iPad, each row has an "Open in New Window" button that calls the `openWindow` environment action with the document's identifier, and the app declares a second `WindowGroup` that presents a document by that identifier. On a test device, tapping the button does nothing. The cause is almost always one of four things, and checking them in order saves time. ## Step 1: can this platform open another window? The `openWindow` action (iOS 16 and later) only works where the platform allows multiple windows. SwiftUI exposes that as the **`supportsMultipleWindows`** environment value, also iOS 16 and later: | Platform and configuration | `supportsMultipleWindows` | |---|---| | macOS, SwiftUI life cycle | `true` | | iPadOS, SwiftUI life cycle, `UIApplicationSupportsMultipleScenes` set to `true` | `true` | | Everything else, including iPhone and iPad without the key | `false` | Apple's documentation states what happens when the value is `false`: SwiftUI **ignores the action and logs a runtime error**. So the first checks are the device and the Info.plist scene manifest. A well-behaved button reads the value and disables itself, which also keeps the iPhone build honest. ## Step 2: is there a scene for this value? `openWindow(value:)` does not name a scene. SwiftUI finds **the window group whose presented type matches the value's type** and passes a binding to that value into the group's content closure. - The type must conform to `Hashable` and `Codable`; the action's signature requires `Decodable`, `Encodable` and `Hashable`. - The group must be declared for exactly that type, for example `WindowGroup(for: UUID.self)` when you pass a `UUID`. Passing a whole model type that happens to be `Codable` does not match a group declared for its identifier. - If two groups present the same type, say a `UUID` for documents and another `UUID` for accounts, give each an `id` and call `openWindow(id:value:)` so SwiftUI can tell them apart. - Apple recommends lightweight values; for `Identifiable` models, the identifier is a good presentation type. ## Step 3: is a window already presenting it? If a window from the group already presents the same value, **the system brings that window to the front instead of opening a new one**. On a crowded iPad this can look like "nothing happened" when the existing window was already visible. This is intended behaviour, not a bug: one window per document. ## Step 4: nil values, defaults and restoration The content closure receives a `Binding` to an **optional** value, and some paths deliver `nil`: - Opening a window from the group without a value, such as the macOS File > New Window command, passes a binding to `nil`. - SwiftUI persists the bound value for **state restoration** and reapplies it when restoring the window; if restoration fails, it sets the binding to the default value if you provided one, or `nil` otherwise. A content view that force-unwraps the value, or shows an empty screen for `nil`, makes these paths look broken. Either handle `nil` explicitly or use the initializer with a `defaultValue` closure, which turns the binding non-optional. ## Closing windows From inside a window, the `dismiss` environment action closes it, except when called from a sheet or popover presented by the window, where it dismisses that modal instead. To close a window by its scene identifier or value, use the `dismissWindow` action, available since iOS 17. ## Confirming the diagnosis - Run from Xcode and read the console: when the platform does not support multiple windows, SwiftUI logs a runtime error as it ignores the call. - Log `supportsMultipleWindows` from the button's view on the failing device; a `false` there settles step 1 at once. - Temporarily call `openWindow(id:)` against a group with a known identifier; if that opens a window, the problem is the value's type rather than the platform. - Close every window of the group and try again, to rule out the bring-to-front case. ## A checklist 1. Read `supportsMultipleWindows`; confirm the device is an iPad and the scene-manifest key is `true`. 2. Confirm a `WindowGroup(for:)` declares the exact type you pass, and that it is `Hashable` and `Codable`. 3. Add an `id` when two groups share a type, and call `openWindow(id:value:)`. 4. Check whether a window for that value is already open. 5. Make the content handle `nil`, or supply `defaultValue`, and test restoration after relaunch.

  • Two WindowGroups in a SwiftUI app both present UUID values, one for documents and one for accounts. How do you open the right one?
    Give each group a unique id, for example WindowGroup("Document", id: "document", for: UUID.self), and call openWindow(id:value:) with both the identifier and the value. The value type alone cannot tell the two groups apart, so the identifier selects the scene and the value selects what it shows.
  • How does a SwiftUI window close itself programmatically on iPadOS?
    From inside the window, call the dismiss environment action; called from a sheet or popover, it dismisses that modal instead of the window. To close a window by its scene identifier or presented value, use the dismissWindow action, available since iOS 17.

  • On iPhone, openWindow opens a new full-screen window over the old one.
  • Any Hashable value can be passed to openWindow(value:).
  • Calling openWindow twice with the same value always creates two windows.
  • The window's content can assume the bound value is never nil.
  • Passing the whole model object is better than its identifier because the window gets all the data.