State restoration per window, and the scene lifecycle
Each window of your app is a scene with its own session. A session outlives its scene: the system can let a scene go and keep the session, then reconnect it later with the saved state. On iPhone Duo this matters for two reasons: people can now have several windows of one app, and opening or closing the phone moves a window between displays. Every Swift block on this page typechecks against the iOS 27.1 simulator SDK (see evidence.md).
Fold and unfold inside one window (scroll position, playback, typed text) is duo-continuity's job. This page covers what differs per window.
SwiftUI
What SwiftUI keeps for you
@SceneStoragevalues, per window. Apple's SceneStorage page says each scene has its own storage and that closing a window destroys it. Keep values small and never store secrets.- The value a
WindowGroup(for:)window presents. SwiftUI saves it and passes it back when it restores the window, or the default value if restoring fails (WindowGroup).
Measured on the closed phone:
- A new window started with its own
@SceneStoragevalue (0) while the first window held 7. - After the app was terminated and relaunched, the system reconnected only the last window used (open sessions 2, connected 1).
- Activating the other saved session with UISceneSessionActivationRequest(session:) brought it back with 7.
- SwiftUI saved its windows with its own activity type,
com.apple.SwiftUI.stateRestoration. Treat it as private.
Per-window navigation
import SwiftUI
struct Note: Identifiable, Hashable, Codable, Sendable {
var id = UUID()
var title: String
}
/// The root of one window. Each window saves its own tab and path.
struct NotesWindowRoot: View {
@SceneStorage("notes.tab") private var tab = 0
@SceneStorage("notes.path") private var savedPath: Data?
@State private var path: [Note.ID] = []
@Environment(\.scenePhase) private var scenePhase
var body: some View {
TabView(selection: $tab) {
Tab("Notes", systemImage: "note.text", value: 0) {
NavigationStack(path: $path) {
Text("Notes")
.navigationDestination(for: Note.ID.self) { Text($0.uuidString) }
}
}
Tab("Search", systemImage: "magnifyingglass", value: 1) { Text("Search") }
}
.task { // runs once: this root keeps its identity
if let savedPath, let ids = try? JSONDecoder().decode([Note.ID].self, from: savedPath) {
path = ids
}
}
.onChange(of: scenePhase) { _, phase in
if phase == .background { savedPath = try? JSONEncoder().encode(path) }
}
}
}- A window opened with
openWindow(value:)already knows its note from the value. Use@SceneStoragefor what changes after that. - The save runs when scenePhase reaches
.background, not on.inactive. duo-continuityhas theNavigationPathversion and the rules for model ownership.
UIKit
Save and restore one window
import UIKit
final class NotesSceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var list: NotesListController?
static let browsingType = "com.example.notes.browsing" // list it under NSUserActivityTypes
func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
// A new window gets the request's activity; a restored window gets its saved one.
let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity
let list = NotesListController()
list.selectedNoteID = (activity?.userInfo?["noteID"] as? String).flatMap(UUID.init(uuidString:))
self.list = list
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UINavigationController(rootViewController: list)
window.makeKeyAndVisible()
self.window = window
}
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
let activity = NSUserActivity(activityType: Self.browsingType)
if let id = list?.selectedNoteID {
activity.targetContentIdentifier = id.uuidString
activity.userInfo = ["noteID": id.uuidString]
}
return activity
}
}
final class NotesListController: UITableViewController {
var selectedNoteID: UUID?
}- The system asks for the activity before it disconnects the scene and stores a copy in
session.stateRestorationActivity(doc). That doc also says the value can benilat connection time if the data is still locked, and is filled in later. - Small per-window values that aren't worth an activity can go in session.userInfo.
Bring back a window instead of duplicating it
import UIKit
@MainActor
func showNote(_ noteID: UUID, otherwise showHere: @escaping @MainActor @Sendable () -> Void) {
let app = UIApplication.shared
let existing = app.openSessions.first {
$0.stateRestorationActivity?.targetContentIdentifier == noteID.uuidString
}
let request: UISceneSessionActivationRequest
if let existing {
request = UISceneSessionActivationRequest(session: existing)
} else {
let activity = NSUserActivity(activityType: "com.example.notes.open")
activity.targetContentIdentifier = noteID.uuidString
activity.userInfo = ["noteID": noteID.uuidString]
request = UISceneSessionActivationRequest(userActivity: activity)
}
app.activateSceneSession(for: request) { _ in
Task { @MainActor in showHere() }
}
}openSessions includes windows whose scene isn't connected. Measured: re-activating such a session reconnected it with its saved state.
Clean up closed windows
When people close windows, UIKit reports the discarded sessions to the app delegate (application(_:didDiscardSceneSessions:)). Delete any per-window files there, but only for your own windows.
import UIKit
final class NotesAppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
for session in sceneSessions where session.role == .windowApplication {
WindowFiles.remove(for: session.persistentIdentifier)
}
}
}
enum WindowFiles {
static func remove(for sessionID: String) {
let url = URL.applicationSupportDirectory.appending(path: "windows/\(sessionID).json")
try? FileManager.default.removeItem(at: url)
}
}Measured: the callback fired for camera capture accessory sessions each time the accessory went away, which is why the loop filters on role. Unverified: how people close one of your windows on iPhone Duo; no app window was discarded in our runs.
The lifecycle on fold and unfold
Apple says opening and closing the phone work like resizing the app in iPhone Mirroring (Tech Talk 111461), and that the app still resizes on open and close even with UIRequiresFullScreen (same talk). Apple's page for windowScene(_:didUpdateEffectiveGeometry:) (iOS 26) and the UIKit header both say it is always called when a window scene moves between screens. Use that callback, or plain trait and size changes, to react. Read the screen from the scene when you need it.
import UIKit
final class PlayerSceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var displayLink: CADisplayLink?
private var renderSize: CGSize = .zero // the size your renderer is set up for
func windowScene(_ windowScene: UIWindowScene,
didUpdateEffectiveGeometry previousEffectiveGeometry: UIWindowScene.Geometry) {
// Background windows can get resized too, with no fold. Skip them here.
guard windowScene.activationState == .foregroundActive else { return }
applyCurrentSize(of: windowScene)
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Catch up on a size change that arrived while the window wasn't active.
guard let windowScene = scene as? UIWindowScene else { return }
applyCurrentSize(of: windowScene)
}
private func applyCurrentSize(of windowScene: UIWindowScene) {
let size = windowScene.effectiveGeometry.coordinateSpace.bounds.size
guard size != renderSize else { return }
renderSize = size
// Layout follows traits on its own; adjust only what doesn't, such as a render target.
}
func startFrameUpdates(in windowScene: UIWindowScene) {
if #available(iOS 27.0, *) {
// Follows the scene when it moves to the other display.
displayLink = windowScene.displayLink(target: self, selector: #selector(step))
} else {
displayLink = CADisplayLink(target: self, selector: #selector(step))
}
displayLink?.add(to: .main, forMode: .common)
}
@objc private func step(_ link: CADisplayLink) {
// Draw one frame.
}
func sceneDidEnterBackground(_ scene: UIScene) {
displayLink?.invalidate()
displayLink = nil
}
}UIWindowScene.displayLink(target:selector:)is iOS 27.0. Apple's doc says the link is retargeted when the scene moves between displays (doc).UIScreen's displayLink(withTarget:selector:) is deprecated in iOS 27 (UIScreen.hpoints to the window scene's version).windowScene.screenchanges when the window moves. Never keep aUIScreenin a property.duo-layouthas the wider rule.- Skipping inactive scenes in the geometry callback isn't enough on its own. A window can change size while it isn't active, and we don't know that UIKit sends another update when it becomes active. So read
effectiveGeometryagain in sceneDidBecomeActive(_:). Unverified: the order of the geometry update and activation when a background window comes back on a closed phone.
SwiftUI: a size-class change is not a fold
Measured: in five of six runs, a window that went to the background got resized and back (table below). On the open phone its width class goes regular, compact, regular within a tenth of a second. Code that reads a width-class change as "the phone folded" would run each time the app leaves the screen.
import SwiftUI
/// Reacts to a real width change only while people can see this window.
struct FoldAwareWindowRoot: View {
@Environment(\.horizontalSizeClass) private var widthClass
@Environment(\.scenePhase) private var scenePhase
@State private var appliedWidthClass: UserInterfaceSizeClass?
var body: some View {
NotesWindowRoot() // one view tree for every width (above)
.onChange(of: widthClass, initial: true) { applyIfActive() }
.onChange(of: scenePhase) { applyIfActive() } // catch up when the window is active again
}
private func applyIfActive() {
guard scenePhase == .active, widthClass != appliedWidthClass else { return }
appliedWidthClass = widthClass
// First call: the starting width. Later calls: the width changed while on screen,
// for example on a fold or an unfold. Adjust what doesn't follow layout on its own.
}
}- Don't swap whole view trees with
if/elseon the size class. The background round trip would rebuild them and drop their@State. Change the layout inside one tree;duo-continuityhas the patterns. - onChange(of:initial:_:) is iOS 17 and later.
What we saw, and what we couldn't check
Measured on the simulator, one window in front, pressing Closed and Open in Device Hub:
| Step | Scene events | SwiftUI |
|---|---|---|
| Open to closed | One didUpdateEffectiveGeometry: 951 × 669 pt on the inner screen to 466 × 678 pt on the outer screen. No resign or become-active calls. Same session. |
Phase stayed active. Width class regular to compact. No new onAppear; @State kept. |
| Closed to open | One didUpdateEffectiveGeometry back to 951 × 669 pt on the inner screen |
Phase stayed active. Root sizes 466 × 562, 951 × 553, then 867 × 635 pt within a tenth of a second. |
So a fold is a resize of the same scene, as Apple says. Your window code doesn't restart.
Measured when a window goes to the background, whether a second window covers it or the whole app leaves the screen. Seen closed and open, in five runs. In a sixth, the covered window got no geometry update in the second before the log ended.
| Order | The window going to the background |
|---|---|
| 1 | sceneWillResignActive(_:), SwiftUI phase active to inactive |
| 2 | sceneDidEnterBackground(_:), phase inactive to background; the scene stayed connected |
| 3 | Two didUpdateEffectiveGeometry calls within 0.2 s, on the same screen, with no fold. Closed: 466 × 678 to 951 × 669 pt (landscape) and back; width class stayed compact. Open: 951 × 669 to 466 × 678 pt (portrait) and back; width class regular to compact to regular. |
Apple doesn't document this resize. Treat any size or size-class change in a scene that isn't active as noise, and read the size again when it becomes active.
When a second window opened on the closed phone, it laid out first at 466 × 562 pt and then at 382 × 644 pt (its root view inside the safe area).
Unverified:
- which window stays on screen when the phone closes with two windows in Split View, and what happens to the other one (Split View needs a drag in Device Hub, which we didn't script);
- Partially Open (we pressed only Closed and Open);
- all of it on a real iPhone Duo.
Run the probe in evidence.md, set up Split View by hand, press the poses, and read the log. Meanwhile:
- Save on
.background. Never tear down (stop playback, clear drafts, release the UI) on.inactive. - Check activationState before doing work in a geometry callback, and catch up in
sceneDidBecomeActive(_:). In SwiftUI, act on size-class changes only whilescenePhase == .active. - Handle sceneDidDisconnect(_:) at any point: UIKit can disconnect a background scene to reclaim resources and keep its session (Managing your app's life cycle).