Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
UIKit  ›  Lesson

The App Lifecycle

View Controllers 12 minAn iOS app moves between not running, inactive, active, background and suspended states, and must save its data on entering the background because suspended apps can be ended silently.

The App Lifecycle

A hiker is logging a hike in TrailLog when a phone call comes in. They answer, then check messages, then come back ten minutes later — or never do, because iOS quietly ended the app to free memory. If TrailLog did not save in time, the half-written hike is gone.

The rule

Active, inactive, background, suspended — save before you leave the foreground.

An iOS app moves through a small set of states:

State What it means
Not running Not launched, or ended by the system
Inactive On screen but not receiving events — a call banner, Control Centre, a moment during launch
Active On screen and receiving touches — the normal state
Background Off screen, still running code for a short time
Suspended In memory but frozen; no code runs. iOS may end it at any moment, with no warning

The key fact is the last row: a suspended app can be terminated without being told. Anything not saved before the app reached the background may be lost.

In a UIKit app, two objects receive lifecycle events:

  • AppDelegate — process-wide events, most importantly application(_:didFinishLaunchingWithOptions:), called once at launch for setup such as configuring services.
  • SceneDelegate — events for one window (iOS 13 and later, when apps gained multiple windows on iPad):
func sceneDidBecomeActive(_ scene: UIScene) { resumeTracking() }
func sceneWillResignActive(_ scene: UIScene) { pauseTimer() }
func sceneDidEnterBackground(_ scene: UIScene) { journal.saveDraft() }
func sceneWillEnterForeground(_ scene: UIScene) { refreshWeather() }

SwiftUI apps see the same states through the scenePhase environment value: .active, .inactive and .background.

The shortcut

For any work, ask "When must this start, and when must it be safe?" Start things in became active, pause them in will resign active, and persist in did enter background — the last reliable moment.

TrailLog and an incoming callStep 1 of 4
  1. Call banner appears

    sceneWillResignActive → pause the hike timer. The app is inactive but still visible.

The trap is saving only when the user taps "Save". Users switch apps constantly, and suspended apps can vanish.

Drills

Answer fast — trust the rule.

quick drill

In which state can iOS end your app with no further notice?

quick drill

Where is the last reliable moment to save TrailLog's draft hike?

quick drill

A notification banner slides down while TrailLog is open. Which state is the app in?

quick drill

Which object handles one window's lifecycle in a modern UIKit app?

Remember

  • Active, inactive, background, suspended — and suspended apps can vanish silently.
  • Start in became active, pause in will resign active, save in did enter background.
  • AppDelegate is the process; SceneDelegate is one window; SwiftUI uses scenePhase.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainNever lose a draft

TrailLog records a hike with a running timer and a draft note. Explain which scene delegate methods should pause the timer, save the draft, refresh stale data and resume, and why saving only when the user taps Save is not enough.