iOS Interview - Explain the iOS App Lifecycle

November 15, 20224 min read#ios, #interview

Question

What Are iOS App Lifecycle Methods?

Answer

The application life cycle constitutes the sequence of events that occurs between the launch and termination of application.

UIKit-based app lifecyle

iOS <= 12

In iOS 12 and earlier, and in apps that don’t support scenes, UIKit delivers all life-cycle events to the UIApplicationDelegate object. The app delegate manages all of your app’s windows, including those displayed on separate screens. As a result, app state transitions affect your app’s entire UI, including content on external displays.

App State

Respond to app-based life-cycle events

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {        
        return true
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {}
    
    func applicationDidBecomeActive(_ application: UIApplication) {}
    
    func applicationWillResignActive(_ application: UIApplication) {}
    
    func applicationDidEnterBackground(_ application: UIApplication) {}
    
    func applicationWillTerminate(_ application: UIApplication) {}

}

API Docs:

iOS >= 13

If your app supports scenes, UIKit delivers separate life-cycle events for each. A scene represents one instance of your app’s UI running on a device. The user can create multiple scenes for each app, and show and hide them separately. Because each scene has its own life cycle, each can be in a different state of execution. For example, one scene might be in the foreground while others are in the background or are suspended.

Scene State

Respond to scene-based life-cycle events

import UIKit

@main
class AppDelegate: UIApplicationDelegate {
    func application(_:didFinishLaunchingWithOptions:) -> Bool {}
    
    func application(_:configurationForConnecting:options:) -> UISceneConfiguration {}
    
    func application(_:didDiscardSceneSessions:) {}
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {}

    func sceneDidDisconnect(_ scene: UIScene) {}

    func sceneDidBecomeActive(_ scene: UIScene) {}

    func sceneWillResignActive(_ scene: UIScene) {}

    func sceneWillEnterForeground(_ scene: UIScene) {}

    func sceneDidEnterBackground(_ scene: UIScene) {}
}

API Docs:

SwiftUI-based app lifecycle

iOS <= 13

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Your code here")
        return true
    }
}

@main
struct MainApp: App {
    // Attach your AppDelegate to your SwiftUI app using 
    // UIApplicationDelegateAdaptor property wrapper
    // SwiftUI is responsible for creating that delegate 
    // and looking after its lifetime, 
    // so you can go ahead and add any other app delegate 
    // functionality to that class to have it called.
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

iOS >=14

@main
struct MainApp: App {
    // The system moves your app’s Scene instances through 
    // phases that reflect a scene’s operational state.
    // Observe scenePhase by using Environment property wrapper.
    @Environment(\.scenePhase) var scenePhase
  
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        // React to changes of scenaPhase by using onChange
        .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
                case .active:
                    print("App is active")
                case .inactive:
                    print("App is inactive")
                case .background:
                    print("App is in background")
                @unknown default:
                    print("Oh - interesting: I received an unexpected new value.")
                }
            }
        }
    }
}

API Docs:

Resources:


Profile picture

Personal blog by An Tran. I'm focusing on creating useful mobile apps.
#Swift #Kotlin #Mobile #MachineLearning #Minimalist


© An Tran - 2022