Sitemap
Pradeep Kumar

Pradeep Kumar

AppDelegate vs SceneDelegate

App States and Application Life Cycle

4 min readSep 28, 2024

--

AppDelegate:

  • Before iOS 13, the AppDelegate managed the entire application lifecycle, including app launch, backgrounding, and termination.
  • It still handles application-wide tasks like registering for push notifications, responding to system-level events (e.g., low memory warnings), and configuring app-wide settings.
  • Key methods include application(_:didFinishLaunchingWithOptions:) and applicationWillTerminate(_:).

SceneDelegate (iOS 13+):

  • Introduced in iOS 13 to support multi-window environments (especially for iPadOS), where apps can have multiple independent UI instances (scenes).
  • It manages scene-specific lifecycle events like scene creation, foregrounding, and backgrounding. Each window or scene can have its own SceneDelegate.
  • Key methods include scene(_:willConnectTo:), sceneDidBecomeActive(_:), and sceneDidEnterBackground(_:).

Key Difference:

  • AppDelegate manages global app-wide events.
  • SceneDelegate manages the lifecycle of individual UI scenes (windows) in apps that support multi-window functionality.

Push Notifications:

  • Push notifications are still mainly handled in the AppDelegate for registration and background delivery.
  • SceneDelegate handles notifications when the app is active in the foreground.

Why SceneDelegate was introduced:

  • You could briefly mention that SceneDelegate was introduced to improve multitasking support, especially for iPadOS, allowing users to open multiple windows of the same app.

Backward compatibility:

  • Mention that for apps targeting iOS 12 and below, only AppDelegate is used, and developers need to ensure backward compatibility if they're supporting older iOS versions.

Example scenarios:

  • You could provide a real-world example, like managing multiple windows in a note-taking app, where SceneDelegate would handle different instances of the app, while AppDelegate manages global tasks like push notification registration.

Get Pradeep Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

Application Life Cycle Process:

App Launch:

  • didFinishLaunchingWithOptions (AppDelegate) - Called when the app has finished launching.
  • willConnectTo (SceneDelegate) - Called when the scene is about to be connected and set up.
  • viewDidLoad (ViewController) - Called after the view controller's view has been loaded into memory.
  • viewWillAppear (ViewController) - Called just before the view becomes visible.
  • sceneWillEnterForeground (SceneDelegate) - Called when the scene moves from the background to the foreground.
  • sceneDidBecomeActive (SceneDelegate) - Called when the scene becomes active (user can interact with the app).
  • viewDidAppear (ViewController) - Called after the view has been presented on screen.

When the app goes to the background:

  • sceneWillResignActive (SceneDelegate) - Called when the app is about to become inactive (e.g., when the user minimizes the app).
  • sceneDidEnterBackground (SceneDelegate) - Called when the app has entered the background.

When the app comes to the foreground from the background:

  • sceneWillEnterForeground (SceneDelegate) - Called when the app is about to move from the background to the foreground.
  • sceneDidBecomeActive (SceneDelegate) - Called when the app becomes active again.

When the app is minimised:

sceneWillResignActive (SceneDelegate) - Called when the app is going into an inactive state (when minimised, not fully backgrounded yet).

When the app is killed (terminated):

  • sceneDidEnterBackground (SceneDelegate) - Called when the app has entered the background.
  • sceneDidDisconnect (SceneDelegate) - Called when the scene is being released, and this usually occurs when the app is terminated or its scene is discarded.

App States in iOS

In Swift, app state refers to the different stages of an iOS app’s lifecycle.

Key States in App Lifecycle:

An iOS app can be in one of five key states at any given time:

  1. Not Running: The app is not running at all. This occurs when the app hasn’t been launched or has been terminated by the system or user.
  2. Inactive: The app is running but is not receiving user input. This happens during transitions, such as when a phone call comes in, or when the user brings up the control center.
  3. Active: The app is running in the foreground and is receiving user events. This is the state where the app is fully functional and interactive.
  4. Background: The app is running in the background but is not visible. Background apps can still execute code, such as completing a file upload or download, for a limited time.
  5. Suspended: The app is in the background but not executing code. The system automatically suspends the app to save memory when no background execution is required.
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

// Called when the app has launched
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}

// Called when the app is about to move from active to inactive state
func applicationWillResignActive(_ application: UIApplication) {
// Pause ongoing tasks, disable timers, etc.
}

// Called when the app has entered the background
func applicationDidEnterBackground(_ application: UIApplication) {
// Release shared resources, save user data, invalidate timers, etc.
}

// Called when the app is about to enter the foreground
func applicationWillEnterForeground(_ application: UIApplication) {
// Undo changes made when entering the background
}

// Called when the app has become active again
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused
}
}
import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?

// Called when the scene is being created
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure the new scene
}

// Called when the scene is about to become inactive
func sceneWillResignActive(_ scene: UIScene) {
// Pause ongoing tasks, disable timers, etc.
}

// Called when the scene has entered the background
func sceneDidEnterBackground(_ scene: UIScene) {
// Release shared resources, save user data, etc.
}

// Called when the scene has moved to the foreground
func sceneWillEnterForeground(_ scene: UIScene) {
// Undo changes made when entering the background
}

// Called when the scene has become active
func sceneDidBecomeActive(_ scene: UIScene) {
// Restart any tasks that were paused
}
}

--

--