skills/generators/state-restoration/SKILL.md
Generates state preservation and restoration infrastructure for navigation paths, tab selection, scroll positions, and form data across app launches and background termination. Use when user wants to save/restore app state, remember where the user left off, or persist UI state.
npx skillsauth add rshankras/claude-code-apple-skills state-restorationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Generate production state restoration infrastructure that saves and restores app state (selected tab, scroll position, navigation path, form data) across launches and background termination. Uses Codable state models, @SceneStorage, @AppStorage, and custom file-based persistence.
Use this skill when the user:
Search for existing state restoration:
Glob: **/*StateRestoration*.swift, **/*AppState*.swift, **/*SceneStorage*.swift
Grep: "SceneStorage" or "NavigationPath" or "selectedTab" or "scrollPosition"
If existing state management found:
Determine which navigation pattern the app uses:
Grep: "NavigationStack" or "NavigationSplitView" or "TabView" or "NavigationLink"
This affects which restoration components to generate.
Ask user via AskUserQuestion:
What state to restore? (multi-select)
Storage method?
Restore behavior?
Read templates.md for production Swift code.
Generate these files:
AppState.swift — Codable struct capturing all restorable stateStateRestorationManager.swift — @Observable manager with auto-save and restoreBased on configuration:
3. NavigationStateModifier.swift — If navigation path selected
4. ScrollRestorationModifier.swift — If scroll position selected
5. TabRestorationModifier.swift — If tab selection selected
6. FormDraftManager.swift — If form data selected
Check project structure:
Sources/ exists -> Sources/StateRestoration/App/ exists -> App/StateRestoration/StateRestoration/After generation, provide:
StateRestoration/
├── AppState.swift # Codable state model
├── StateRestorationManager.swift # Auto-save/restore orchestrator
├── NavigationStateModifier.swift # Navigation path persistence (optional)
├── ScrollRestorationModifier.swift # Scroll position persistence (optional)
├── TabRestorationModifier.swift # Tab selection persistence (optional)
└── FormDraftManager.swift # Form draft auto-save (optional)
Basic setup in App struct:
@main
struct MyApp: App {
@State private var stateManager = StateRestorationManager()
var body: some Scene {
WindowGroup {
ContentView()
.environment(stateManager)
}
}
}
Restore navigation path:
struct ContentView: View {
@Environment(StateRestorationManager.self) private var stateManager
var body: some View {
@Bindable var sm = stateManager
NavigationStack(path: $sm.navigationPath) {
HomeView()
.navigationDestination(for: Route.self) { route in
RouteView(route: route)
}
}
.modifier(NavigationStateModifier(stateManager: stateManager))
}
}
Restore tab selection:
struct MainTabView: View {
@Environment(StateRestorationManager.self) private var stateManager
var body: some View {
@Bindable var sm = stateManager
TabView(selection: $sm.selectedTab) {
HomeTab().tag(0)
SearchTab().tag(1)
ProfileTab().tag(2)
}
.modifier(TabRestorationModifier(stateManager: stateManager))
}
}
Restore scroll position:
struct ItemListView: View {
let items: [Item]
var body: some View {
ScrollView {
LazyVStack {
ForEach(items) { item in
ItemRow(item: item)
}
}
}
.modifier(ScrollRestorationModifier(scrollViewID: "item-list"))
}
}
Auto-save form drafts:
struct ComposeView: View {
@State private var draftManager = FormDraftManager(formID: "compose")
@State private var title = ""
@State private var body = ""
var body: some View {
Form {
TextField("Title", text: $title)
TextEditor(text: $body)
}
.onAppear { draftManager.restore(into: &title, &body, keys: "title", "body") }
.onChange(of: title) { draftManager.save(key: "title", value: title) }
.onChange(of: body) { draftManager.save(key: "body", value: body) }
.onSubmit { draftManager.clearDraft() }
}
}
@Test
func stateRestoredFromDisk() async throws {
let manager = StateRestorationManager(storage: .file(directory: tempDir))
manager.selectedTab = 2
manager.saveState()
let restored = StateRestorationManager(storage: .file(directory: tempDir))
restored.restoreState()
#expect(restored.selectedTab == 2)
}
@Test
func timeLimitedRestoreExpires() async throws {
let manager = StateRestorationManager(
storage: .file(directory: tempDir),
restoreBehavior: .timeLimited(minutes: 30)
)
// Simulate state saved 60 minutes ago
manager.appState.lastSavedDate = Date().addingTimeInterval(-3600)
manager.saveState()
let restored = StateRestorationManager(
storage: .file(directory: tempDir),
restoreBehavior: .timeLimited(minutes: 30)
)
restored.restoreState()
#expect(restored.selectedTab == 0) // Default, not restored
}
@Test
func formDraftClearedOnSubmit() async throws {
let draft = FormDraftManager(formID: "test", storage: .file(directory: tempDir))
draft.save(key: "title", value: "My Draft")
#expect(draft.value(for: "title") == "My Draft")
draft.clearDraft()
#expect(draft.value(for: "title") == nil)
}
enum Route: Codable, Hashable {
case detail(id: UUID)
case settings
case profile(userID: String)
}
// NavigationPath supports Codable serialization
let representation = navigationPath.codable
let data = try JSONEncoder().encode(representation)
// Use String tags instead of Int for readability and stability
TabView(selection: $stateManager.selectedTab) {
HomeView().tag("home")
SearchView().tag("search")
ProfileView().tag("profile")
}
// Save draft only after user pauses typing (1 second)
.onChange(of: textContent) {
draftManager.save(key: "content", value: textContent)
// Internally debounced — won't write to disk on every keystroke
}
NavigationPath.CodableRepresentation converted to Data.scenePhase). Force quit from the app switcher does NOT trigger scenePhase change on iOS. Save state on every significant change, not just on backgrounding.stateVersion field for breaking changes.generators/persistence-setup — Core Data / SwiftData persistence layergenerators/deep-linking — URL-based navigation restorationdevelopment
Build, install, and launch an iOS app on a physical iPhone or iPad entirely from the command line (no Xcode GUI), using xcodebuild + devicectl. Use when the user wants to run, test, or screenshot their app on a real device without opening Xcode.
development
Comprehensive iOS development guidance including Swift best practices, SwiftUI patterns, UI/UX review against HIG, and app planning. Use for iOS code review, best practices, accessibility audits, or planning new iOS apps.
development
Build, install, launch, and screenshot an iOS app in the Simulator to verify a change visually. Use when the user wants to run the app, see a change live, screenshot the running app, or confirm a UI fix actually works (not just that it compiles).
development
Audits skills in this repo for consistency, API drift, and structural gaps. Produces a prioritized report grouped by severity (Critical/High/Medium/Low). Use when asked to "audit skills", "check the skill repo for drift", or when planning bulk skill cleanup. Read-only — does not apply fixes.