In SwiftUI, how do you make @AppStorage use an App Group's UserDefaults suite so an extension sees the same settings?
answer
- not UserDefaults.standard
- a suite named after the group
- per property or per hierarchy
- store: versus defaultAppStorage(_:)
basics
~10 sCreate the suite with UserDefaults(suiteName:) using an App Group both targets join, then pass it as @AppStorage's store: argument or apply defaultAppStorage(_:) to a scene or view so every @AppStorage inside uses it.
solid answer
~40 sBy default `@AppStorage` reads `UserDefaults.standard`, which is private to the app, so an extension never sees it. Add the App Groups capability to the app and the extension with the same group, and create the shared store with `UserDefaults(suiteName: "group.com.example.reader")`, which is failable. Then either pass it per property, `@AppStorage("reader.fontSize", store: groupDefaults)`, or set it for a whole subtree with `defaultAppStorage(_:)`, which exists on both `View` and `Scene`; Apple's documentation names sharing defaults with an extension as the use case. An explicit `store:` wins for that property. Values already written to the standard store do not move, so existing users need a one-time copy.
code
swift · 25 linesimport SwiftUI
enum SharedKeys {
static let suiteName = "group.com.example.reader"
static let theme = "reader.theme"
}
@main
struct ReaderApp: App {
var body: some Scene {
WindowGroup {
RootView()
}
.defaultAppStorage(UserDefaults(suiteName: SharedKeys.suiteName) ?? .standard)
}
}
struct RootView: View {
// No store: argument, so this reads the suite set on the scene.
@AppStorage(SharedKeys.theme) private var theme = "system"
var body: some View {
Text("Theme: \(theme)")
}
}go deeper
Know that @AppStorage uses the standard defaults unless you give it another store, and that sharing with a widget needs an App Group suite.
Explain store: versus defaultAppStorage(_:), that the modifier exists on View and Scene, and that an explicit store wins over the hierarchy default.
Bring the production details: a failable suite initializer, scenes and subtrees that miss the modifier, shared key constants, and migrating values out of the standard store.
Decide which settings are shared across targets and which stay app-private, and make that a reviewed list rather than a per-feature choice.
## Why the standard store is not enough `@AppStorage` is SwiftUI's property wrapper over `UserDefaults`. Unless told otherwise, it uses **`UserDefaults.standard`**, the app's own preferences domain. An **app extension** such as a widget runs as a separate process with its own container, so it cannot read the app's standard defaults. When the reading app's widget should render in the reader's chosen theme, both sides need a **shared suite**. The shared suite comes from an **App Group**: a capability you add to every target that should share data, with the same group identifier (by convention `group.` plus a reverse-DNS name). Code in any of those targets can then open the same defaults with `UserDefaults(suiteName:)`. ## Two ways to point @AppStorage at the suite | Approach | Where you write it | Scope | Good for | |---|---|---|---| | `store:` argument | On one `@AppStorage` declaration | That property only | One or two shared keys | | `defaultAppStorage(_:)` on a `Scene` | In the `App` body | Every `@AppStorage` in that scene that names no store | Making the suite the app's default | | `defaultAppStorage(_:)` on a `View` | On any view | Every `@AppStorage` in that subtree that names no store | One feature that uses the suite | Both have been available since iOS 14. Apple's documentation for `defaultAppStorage(_:)` says: if unspecified, the default store for a view hierarchy is `UserDefaults.standard`, and sharing defaults between an app and an extension is the example of overriding it with a store created by `UserDefaults(suiteName:)`. The `store:` parameter is declared as an optional `UserDefaults` defaulting to `nil`. A property that passes a store uses that store; a property that passes nothing uses whatever default the hierarchy provides. ## Setting it up step by step 1. Add the **App Groups** capability to the app target and to the extension target, with the same group identifier. 2. Create the suite: `UserDefaults(suiteName: "group.com.example.reader")`. The initializer is failable, so decide what happens when it returns `nil` (a misconfigured entitlement): fall back to the standard store and log it. 3. Apply it once, high up: `.defaultAppStorage(suite)` on the `WindowGroup` in the app, and on the extension's root view. 4. Declare `@AppStorage("reader.theme")` with the same key in both targets. Keep the key strings in one file both targets compile, so they cannot drift. 5. **Migrate existing users**: values already in the standard store stay there. On first launch after the change, copy each key that is missing from the suite, then remove it from the standard store. ## Pitfalls - **A second scene** declared in the `App` body does not inherit a modifier you applied to the first; apply `defaultAppStorage(_:)` to each scene that needs the suite. - **A view outside the modified subtree** silently uses `UserDefaults.standard`, so the app and the widget disagree with no error. - **Changing a key's store** looks like losing the user's setting, because the new store starts empty and every `@AppStorage` falls back to its declared default. - **The extension does not redraw by itself** when the app writes: a widget renders from its timeline, and asking it to reload is a WidgetKit task, outside SwiftUI's property wrappers. - **The suite is shared, not secure**: anything every target in the group can read is not a place for credentials, which belong in the Keychain. ## Reading the same values outside SwiftUI Not every reader of the suite is a view. A timeline provider or an intent handler reads plain `UserDefaults`, so it must agree with how `@AppStorage` stores each type: - A `Bool`, `Int`, `Double`, `String` or `Data` is stored as that value under the key, so `suite.string(forKey: "reader.theme")` reads what the app wrote. - A `RawRepresentable` enum is stored as its **raw value**, so non-SwiftUI code reads the `String` or `Int` and rebuilds the case with `init(rawValue:)`. - When the key is missing, plain `UserDefaults` accessors return their own fallbacks, such as `nil` or `0`, not the default written in the `@AppStorage` declaration, so define the default once and use it on both paths. ## What to say in the interview A good answer separates the three layers. **App Groups** make a shared container possible; **`UserDefaults(suiteName:)`** opens the shared defaults; **`store:` or `defaultAppStorage(_:)`** tells SwiftUI's wrapper to use it. Then it adds the two production details that show experience: an explicit `store:` beats the hierarchy default, and switching stores needs a migration for users who already have settings.
- A view passes store: to @AppStorage inside a scene that applies defaultAppStorage(_:) with another suite. Which store does it use?The one it passes. `defaultAppStorage(_:)` supplies the store only for `@AppStorage` properties that leave `store:` at its `nil` default. An explicit argument is that property's store, which is useful for one key that must stay app-private while the rest are shared, and a source of bugs when someone passes the wrong suite.
- How do you move existing users' settings from the standard store into the App Group suite?Run a one-time migration at launch, before any view reads the keys: for each known key, if the suite has no value and the standard store does, copy it across, then remove it from the standard store and record that the migration ran. Without it, every user sees the declared defaults after the update.
- defaultAppStorage(_:) changes UserDefaults.standard for the whole process.
- An extension can read the app's UserDefaults.standard values directly.
- Any suite name shares data with an extension, App Group or not.
- Switching @AppStorage to a new store carries existing values over.
- defaultAppStorage(_:) overrides an explicit store: argument.