In SwiftUI, when do you use @SceneStorage instead of @AppStorage, for example to reopen the article each iPad window was showing?
answer
- app-wide versus per-window
- preferences versus UI state
- system saves and restores it
- closing the window discards it
basics
~20 s@AppStorage holds one app-wide value per UserDefaults key, right for preferences like a theme. @SceneStorage holds a per-scene value the system saves and restores, right for UI state like the article an iPad window shows; destroying that scene discards it.
solid answer
~40 sBoth are SwiftUI property wrappers keyed by a string, but they have different scopes. `@AppStorage` reads and writes `UserDefaults`, so every window sees the same value: the reader's font size and theme. `@SceneStorage` works much like `@State`, except the system restores its initial value if it saved one, and each scene has its own storage, so two iPad windows can each reopen a different article. Store a lightweight value such as the article's ID, not the article: Apple says large data such as model data should not go there. The system decides when to save it, the backing data is not accessible to you, and when the scene is destroyed, for example when the person removes its snapshot from the iPad app switcher, its values go with it.
code
swift · 28 linesimport SwiftUI
struct Article: Identifiable {
let id: String
let title: String
let text: String
}
struct ReaderWindowView: View {
@SceneStorage("reader.openArticleID") private var openArticleID: String?
@AppStorage("reader.fontSize") private var fontSize = 17.0
let articles: [Article]
var body: some View {
NavigationSplitView {
List(articles, selection: $openArticleID) { article in
Text(article.title)
}
} detail: {
if let id = openArticleID,
let article = articles.first(where: { $0.id == id }) {
ScrollView { Text(article.text).font(.system(size: fontSize)) }
} else {
ContentUnavailableView("Pick an article", systemImage: "book")
}
}
}
}go deeper
Remember the split: @AppStorage for settings the whole app shares, @SceneStorage for what one window was showing so it comes back after relaunch.
Explain the per-scene scope, the same-key sharing inside one scene, the supported value types, and why you store an ID and look the model up.
Show you know the documented limits: no guarantee when it is saved, data destroyed with the scene, lightweight values only, nothing sensitive, and a fallback when the restored ID is stale.
Tie it to the multi-window experience: decide which UI state each scene restores, keep keys scoped by feature, and keep model data in the data layer.
## Two wrappers, two scopes SwiftUI has two property wrappers that keep a view value beyond the life of the view, and the question is really about **scope** and **purpose**. | | `@AppStorage` | `@SceneStorage` | |---|---|---| | Backing store | `UserDefaults` (standard or a suite you choose) | Per-scene storage the system manages | | Scope | The whole app: every window reads the same value | One scene: each window has its own value | | Typical content | Preferences: font size, theme, a feature toggle | UI state: selected article, selected tab, scroll target ID | | Lifetime | Until the app removes the key or is deleted | Until the scene is destroyed | | Who saves | Each assignment writes to the store | The system, at times it chooses | | Readable elsewhere | Yes, through `UserDefaults` | No, only through the wrapper | A **scene** here is a SwiftUI `Scene`, most often a window created by a `WindowGroup`. On iPhone an app usually has one; on iPad and Mac a person can open several, and each needs to come back the way it was. ## How @SceneStorage behaves Apple's documentation for `SceneStorage` describes it closely: - It "works very similar to `State`, except its initial value is restored by the system if it was previously saved". - The value is **shared with other `@SceneStorage` properties in the same scene** that use the same key, so a sidebar and a detail view can both read the selection. - "Each `Scene` has its own notion of `SceneStorage`, so data is not shared between scenes." - The system manages saving and restoring, the underlying data "is not available to you", and it "makes no guarantees as to when and how often the data will be persisted." - Keep the data **lightweight**: large data such as model data "should not be stored in `SceneStorage`, as poor performance may result." - If the scene is explicitly destroyed (the switcher snapshot is destroyed on iPadOS, or the window is closed on macOS), the data is destroyed too. Apple also says not to use it with sensitive data. ## The reading app: which value goes where 1. The **font size** and **theme** are the reader's preferences and should apply in every window, so they are `@AppStorage`. 2. The **article each window is showing** differs per window and should come back after relaunch, so each window keeps the article's **ID** in `@SceneStorage`. 3. The **articles themselves** are model data. They live in the app's data layer, and the view looks the article up by the restored ID, treating a missing article (deleted since) as "nothing selected". ## Keys and value types `@SceneStorage` accepts the same basic value types as `@AppStorage`: `Bool`, `Int`, `Double`, `String`, `URL`, `Data`, `RawRepresentable` types with a `String` or `Int` raw value, and optionals of them. It has no `store:` parameter for those types, because the system owns the storage. Apple's restoration sample adds advice about keys: - Each key must be **unique and scoped** to the area that uses it, for example `"ContentView.selectedProduct"`. - Because scene storage is local to the app, there is no need to prefix keys with the bundle identifier; use a disambiguating prefix where needed. ## Limits to keep in mind - `@SceneStorage` is not a save mechanism you can rely on second by second: a value set just before the app is killed may not have been persisted. - It is not a cache for content and not a place for anything private. - It does not replace `@AppStorage`: a theme in `@SceneStorage` would differ per window and vanish when the window is closed. - Restoring a navigation stack's whole path is a separate technique, covered with stack-based navigation. - On iPhone, where an app usually shows a single window, `@SceneStorage` still earns its place: it brings back the selected tab or article after the system ends the app in the background, without writing that state into the app-wide preferences. The interview answer that lands names the scope difference first (app-wide versus per-scene), then the purpose (preferences versus UI state), then the two limits Apple documents: keep it lightweight, and do not put sensitive data in it.
- Why store the article's ID in @SceneStorage rather than the whole article?Apple's documentation asks for lightweight values and warns that large data such as model data causes poor performance there. The ID is enough to rebuild the screen: the view looks the article up in the app's data layer, and if it no longer exists, it shows the empty state instead of stale content.
- Do two SwiftUI views in the same window share a @SceneStorage value?Yes, when they use the same key. Apple's documentation says the value is shared with other `@SceneStorage` properties in the same scene, so a list and a detail view can both read and write the selection. A second window is a different scene and gets its own value.
- @SceneStorage values are shared across all windows of the app.
- A preference like the theme belongs in @SceneStorage.
- @SceneStorage data survives the person closing that window.
- Every @SceneStorage write is saved immediately and guaranteed.
- You read @SceneStorage data back through UserDefaults.