appdelegate lifecycle in swift ios
The UIApplicationDelegate (AppDelegate) is the root object of a UIKit-based iOS application, responsible for handling process-level lifecycle events and responding to system-level notifications.
Starting with iOS 13, Apple split lifecycle responsibilities: AppDelegate handles process-level events (like launch and termination), while SceneDelegate handles UI-level lifecycle events (like active or background states for multiple windows).
🔄 The 5 Core Execution States
Not Running: The app has either not been launched or was completely terminated by the user/system.
Inactive: The app is running in the foreground but is not receiving user interaction events (e.g., interrupted by a phone call or pulling down Notification Center).
Active: The app is running in the foreground, visible, and fully accepting user inputs.
Background: The app is out of view but executing code in the background (e.g., playing audio, tracking location)
Suspended: The app remains in device memory but does not execute any code. The system can purge it anytime without notice if memory runs low.
🛠️ Key AppDelegate Lifecycle Methods
- Initializing and Launching
application(_:willFinishLaunchingWithOptions:)
When: The launch process begins. It is the first code executed from your delegate.
Purpose: Use this for initial application setups before state restoration occurs.
application(_:didFinishLaunchingWithOptions:)
When: The launch process is almost complete and the app is about to run.
Purpose: Initialize global SDKs (e.g., Firebase), set up third-party analytics, and configure app-wide styling.
- Transitioning (iOS 12 & Earlier or Apps Without Scenes)
applicationWillResignActive(_:)
When: Moving from Active to Inactive.
Purpose: Pause ongoing timers, lower video frame rates, or pause gameplay.
applicationDidEnterBackground(_:)
When: Moving from Inactive to Background.
Purpose: Release shared resources, write user data to disk, and reduce memory footprint to prevent suspension purging
applicationWillEnterForeground(_:)
When: Transitioning from Background to Inactive state (on its way to becoming active).
Purpose: Undo changes made when entering the background (e.g., load data from network).
applicationDidBecomeActive(_:)
When: The app becomes fully Active.
Purpose: Refresh the UI, restart paused animations, and resume game states.
- Termination
applicationWillTerminate(_:)
When: The app is about to be purged from memory entirely.
Purpose: Save final, critical database states and clear cache files. Note: This method is not called if the app is already in a suspended state when killed.
🧬 SwiftUI and Modern Alternatives
If you are developing a modern application using the SwiftUI App Lifecycle, there is no AppDelegate file by default.
ScenePhase Modifier: You can monitor state transitions directly in your main @main struct using the scenePhase environment property (supporting .active, .inactive, and .background).
@UIApplicationDelegateAdaptor: If you still require old AppDelegate capabilities (like registering for remote APNS push notifications), you can inject a custom delegate class into your SwiftUI struct using the @UIApplicationDelegateAdaptor property wrapper.
In SwiftUI, lifecycle management
Core View Visibility Modifiers
.onAppear(perform:)
.onDisappear(perform:)Asynchronous Operations
.task(priority:_:)
.task(id:)State & Reactive Data Triggers
init() (The Constructor)
onChange(of:perform:)
onReceive(_:perform:)Application-Wide Lifecycle (ScenePhase)
To manage the high-level application states (equivalent to AppDelegate lifecycle events), SwiftUI utilizes scenes and the environment state.
.active: The app is in the foreground and interacting with the user.
.inactive: The app is visible but temporarily interrupted (e.g., control center open, taking a phone call).
.background: The app is completely hidden and suspended.
what asynchronous task
An asynchronous task is an operation that runs in the background. It allows a computer or program to start the task and immediately move on to other work without waiting for that first task to finish.
How it works
Think of ordering food at a fast-food restaurant.
Synchronous (Waiting): You order a burger, and the cashier waits at the register, doing nothing else, until your food is ready to hand to you.
Asynchronous (Not waiting): You order your burger, the cashier gives you a token, and you sit down to read a book while the kitchen cooks. The cashier immediately takes the next person's order.
Why we use it
Asynchronous tasks keep applications fast and responsive. If a computer had to stop and wait for a large file to download or a website to load, the whole program would freeze. Instead, it sends the download to the background and lets you keep clicking and typing.
Common examples
Downloading files: You click "download," and you can still scroll through a webpage while the file loads in the background.
Loading websites: Your app asks the internet for new data, but your screen does not freeze; it shows a loading spinner until the data arrives.
Sending emails: An app sends a confirmation email in the background after you buy something, so you don't have to sit and stare at a loading screen.
Top comments (0)