UIKit and the app lifecycle

5 concepts · 10 practice questions

App and scene lifecycle

The app delegate handles process-wide events such as launch and push notification registration; a scene delegate handles each window's UI lifecycle, because an iPad app can show several scenes at once. A scene moves between active, inactive (visible but not receiving events, as when Control Center is pulled down), background and suspended. A suspended app can be terminated without any further callback, so save the user's work when the scene enters the background. In SwiftUI, scenePhase reports the same states.

final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    func sceneDidBecomeActive(_ scene: UIScene) { player.resumeIfNeeded() }
    func sceneWillResignActive(_ scene: UIScene) { player.pause() }       // Control Center, alerts
    func sceneDidEnterBackground(_ scene: UIScene) { store.saveDraft() }  // may be killed after this
}

// SwiftUI
.onChange(of: scenePhase) { _, phase in
    if phase == .background { store.saveDraft() }
}

Also covered

View controller lifecycle
Auto Layout
Diffable data sources and cell reuse
UIKit and SwiftUI together

Sign in to read the rest and take the 10-question test.

Sign in to continue