App and scene lifecycle states

Platform & distribution Junior Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Predict the output

The user swipes up to go home while your app is active. Which scene callbacks run, in order?

// SceneDelegate
func sceneWillResignActive(_ scene: UIScene) { print("resign") }
func sceneDidEnterBackground(_ scene: UIScene) { print("background") }
func sceneDidDisconnect(_ scene: UIScene) { print("disconnect") }
Options
Hint 1 of 1

Active → ? → background.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A resign, then background

Correct. Active → inactive → background, and later suspended.

Why not the others?
  • B background only. The scene becomes inactive first.
  • C resign, background, disconnect. Disconnect happens later, if the system releases the scene.
  • D disconnect only. Going home doesn't immediately discard the scene.

30-second answer

An app is not running, inactive (in the foreground but not receiving events, for example during a call alert or while transitioning), active, in the background (running code briefly or for an allowed reason), or suspended (in memory but not running). With scenes, each window gets its own callbacks such as sceneDidBecomeActive and sceneDidEnterBackground; SwiftUI exposes them through scenePhase.

Practise saying it · 30 s. No recording, just a timer

Explain the lifecycle states.

Say your answer out loud, then check which of these points you covered:

  • Five states
  • Scene callbacks
  • Save on background

Deep dive

Save state when entering the background; a suspended app can be killed without notice.

Background time is limited; ask for more with beginBackgroundTask or use BGTaskScheduler.

SwiftUI: .onChange(of: scenePhase).

Inactive also happens when Control Center or the app switcher opens.

Common mistakes

  • Saving data only in applicationWillTerminate, which may never run.
  • Treating inactive as background.

Senior take & follow-ups

Seniors explain scene-based lifecycle and save state at the right moment.

Follow-up questions

Think of your answer first, then open the hint.

  1. Is willTerminate reliable?

    Show hint

    No; suspended apps are killed without it.

  2. How does SwiftUI see these states?

    Show hint

    The scenePhase environment value.

Suggest a fix if something here is wrong or out of date.