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 importantlyapplication(_: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.
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.
In which state can iOS end your app with no further notice?
Where is the last reliable moment to save TrailLog's draft hike?
A notification banner slides down while TrailLog is open. Which state is the app in?
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.
AppDelegateis the process;SceneDelegateis one window; SwiftUI usesscenePhase.
