Can you describe the app lifecycle in iOS?

Instruction: Explain the main stages in the lifecycle of an iOS app.

Context: Assessing the candidate's knowledge of the app lifecycle is essential for understanding how iOS apps operate from launch to termination. This includes knowing the states an app can be in, such as active, background, suspended, and the transitions between these states. It reveals the candidate's ability to manage app behavior efficiently across different states.

Updated

Official answer

An iOS process may be not running, active, inactive, in the background or suspended. Active means it can receive user interactions; inactive is a transition or interruption state. A background process has limited permitted work; suspension means its code is not executing. The system may later terminate it.

For a UIKit app that enables scenes on iOS 13 or later, each UIScene has its own lifecycle. One window can be active while another is in the background. The app delegate still handles process-level launch and other app events; scene delegates handle their UI's connection, activation, deactivation, backgrounding and disconnection. Older or non-scene configurations use the app-based callbacks. SwiftUI expresses scene changes through its app/scene lifecycle and scenePhase.

At connection, build the scene's UI and restore appropriate state. On activation, refresh stale information and resume work needed by that visible UI. On deactivation/backgrounding, persist valuable changes, pause unnecessary timers/rendering, protect sensitive snapshot content and release recreatable resources. A scene disconnect is not necessarily deletion of its saved session.

Persist important edits incrementally rather than waiting for applicationWillTerminate: a suspended process can disappear without that callback. Background execution is permission- and task-specific, with expiration/cancellation handling; dispatching to a background queue does not grant unlimited background runtime. Test interruption, multiple windows, lock/unlock, restoration and termination during pending work.

Scene and app lifecycle, Termination callback limits

Related Questions