In SwiftUI, how does the scenePhase environment value differ when read in a view versus in the App, and where do you save edits?
answer
- three phases, one enum
- a view sees its own scene
- the App sees an aggregate
- onChange on the phase value
basics
~20 sRead 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.
solid answer
~40 s`ScenePhase` has three cases, `.active`, `.inactive` and `.background`, read with `@Environment(\.scenePhase)`. Where you read it changes its meaning. Inside a view it reflects the scene that contains the view, so each window of a document editor sees its own phase. Inside the `App` it is an aggregate: `.active` if any scene is active, `.inactive` when none is, and `.background` only once all of them have gone there; Apple's documentation adds that when the app reaches the background you should expect it to terminate soon after. So per-window work, like saving the draft that window edits, goes in the editor view with `onChange(of: scenePhase)`, and app-wide work, like flushing a shared cache, goes on the `WindowGroup` in the `App`. Apple recommends this over app delegate callbacks.
code
swift · 23 linesimport Observation
import SwiftUI
@Observable
final class Draft {
var text = ""
func save() { /* write the text to disk */ }
}
struct EditorView: View {
@Environment(\.scenePhase) private var scenePhase
@Bindable var draft: Draft
var body: some View {
TextEditor(text: $draft.text)
.onChange(of: scenePhase) { oldPhase, newPhase in
// This window's own scene, not the whole app.
if newPhase == .background {
draft.save()
}
}
}
}go deeper
Recall the three ScenePhase cases and that you read the value with @Environment(.scenePhase) and react with onChange.
Explain the two readings: a view sees its own scene, the App sees an aggregate, and say which work belongs at each level.
Show the multi-window consequence: App-level saves stop firing per window, so per-window data must be saved from the window's views.
Weigh saving on inactive versus background against write cost and data loss, and set one convention the whole codebase follows.
## The three phases SwiftUI describes a scene's operational state with the **`ScenePhase`** enum, available since iOS 14. You read it from the environment with `@Environment(\.scenePhase)`. Apple's documentation gives each case a one-line meaning: | Case | Meaning in Apple's documentation | |---|---| | `.active` | The scene is in the foreground and interactive | | `.inactive` | The scene is in the foreground but should pause its work | | `.background` | The scene isn't currently visible in the UI | `ScenePhase` is `Comparable` and `Sendable`, but the useful operation is equality: you compare the current value with a case and act on the change. ## Where you read it decides what it means The same environment key answers two different questions depending on who reads it. - **Inside a view** it reports the phase of **the scene that contains the view**. In an iPad app with two windows, a view in the left window and a view in the right window can see different phases at the same moment. - **Inside the `App`** it reports an **aggregate** of every scene in the app, including all the windows created from one `WindowGroup`. The app reports `.active` if any scene is active and `.inactive` when no scene is active. It reaches `.background` only when all scenes are in the background, and Apple's documentation adds that at that point you should expect the app to terminate soon after, so it is the moment to free resources. This distinction is the heart of the question. Code that only works when there is one window, such as an App-level save on `.background`, quietly stops matching what people see once multiple windows are enabled. ## Saving a document editor's work For a document editor, unsaved text belongs to a specific window, so the save belongs in that window's view hierarchy: 1. Read `@Environment(\.scenePhase)` in the editor view that owns or receives the draft. 2. Attach `onChange(of: scenePhase)` using the iOS 17 two-parameter or zero-parameter form; the one-parameter `onChange(of:perform:)` form is deprecated since iOS 17. 3. When the new phase is `.background`, or when it leaves `.active` if you want to save earlier, write the draft. 4. Keep the save fast and idempotent, because it can run more than once as the scene moves between phases. Saving on `.inactive` is a judgment call: Apple describes it as a foreground scene that should pause its work, so committing edits there is safe, at the cost of more frequent writes. Saving only on `.background` writes less often but leaves edits unsaved for longer. ## App-level work The aggregate value is the right signal for work that concerns the whole process rather than one window: flushing a shared cache, closing a shared connection, or releasing memory-heavy resources. Attach `onChange(of: scenePhase)` to the scene in the `App` body; `Scene` has the same `onChange(of:initial:_:)` modifier as views since iOS 17. A custom `Scene` type can read the value as well. Apple's own example declares a scene that owns a cache and empties it when all of the scenes in its window group have moved to the background, which is the same aggregate reasoning applied one level down. Whichever level you choose, write down why: a reviewer reading `onChange(of: scenePhase)` cannot tell from the call alone whether it reacts to one window or to all of them. ## Why not the app delegate An app that uses the SwiftUI life cycle can still have a UIKit app delegate through an adaptor, but Apple's documentation is direct: manage life cycle events without an app delegate whenever possible, and prefer to handle changes in `ScenePhase` instead of relying on delegate callbacks. The migration guide also notes that, because the SwiftUI life cycle is scene-based, you can no longer monitor life cycle changes in the app delegate the way a UIKit app did. Scene phase gives you per-window precision that an app-wide callback cannot. ## Pitfalls - Saving per-window data from the App-level handler, which never fires while another window is still active. - Treating `.inactive` as "the app was killed"; the scene is still in the foreground. - Using the deprecated one-parameter `onChange` closure in new code. - Doing slow, blocking work in the handler, which runs on the main actor because views and apps are main-actor isolated.
- Why might an App-level onChange(of: scenePhase) that saves on .background never fire while one of two iPad windows goes to the background?Because the App reads an aggregate: it stays active while any scene is active and reports .background only once every scene is there. One window going to the background changes that window's scene phase, which only views inside that window see. Save per window from the editor view, and keep the App-level handler for work that concerns all windows.
- Is .inactive a good moment to save a document editor's draft?It is a reasonable one. Apple describes .inactive as a scene still in the foreground that should pause its work, so committing edits there is safe. Saving there and again on .background costs extra writes but leaves less unsaved; saving only on .background writes less. Either way, make the save quick and idempotent.
- scenePhase read in the App always matches the frontmost window's phase.
- A view's scenePhase reports the whole app's state rather than its own window's.
- The .inactive phase means the app has already been terminated.
- Phase changes can only be observed through app delegate callbacks in SwiftUI.
- The one-parameter onChange(of:perform:) closure is the current form on iOS 17 and later.