SwiftUI AppStorage & SceneStorage
Persist small values with @AppStorage and view-state per scene with @SceneStorage.
@AppStorage (User Defaults)
Bind a value directly to UserDefaults so changes persist across app launches.
Syntax: @AppStorage("key") var value: T = default
Example
Demo.swift
ContentView.swift
App.swift
import SwiftUI
struct AppStorageDemo: View {
@AppStorage("username") private var username = ""
var body: some View {
VStack(spacing: 12) {
Text("Hello, \(username.isEmpty ? "Guest" : username)")
TextField("Username", text: $username)
.textFieldStyle(.roundedBorder)
}
.padding()
}
}
This example stores the username in UserDefaults and keeps the UI in sync.
@SceneStorage (Per-Scene UI State)
Preserve transient UI data (like a draft) per window/scene; state is restored when the scene returns.
Syntax: @SceneStorage("key") var value: T
Example
Demo.swift
ContentView.swift
App.swift
import SwiftUI
struct SceneStorageDemo: View {
@SceneStorage("draft_note") private var draft = ""
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Draft:")
TextEditor(text: $draft)
.frame(minHeight: 120)
.overlay(RoundedRectangle(cornerRadius: 6).stroke(.secondary))
}
.padding()
}
}
Leave a Reply