Application Lifecycle in iOS
Tags: swift, ios, programming
Published: 2022-04-25 11:12:23

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

##Steps involved in device reboot to App launch:
- When a device reboots no apps will be running except the apps that belongs to OS.
- When a user touches the app icon on the screen,The SpringBoard launches the app.

> SpringBoard is the standard application that manages the iPhone’s home screen.

- While SpringBoot animates your app's launch screen, your app and necessary shared libraries will be loaded in the memory. Eventually your app begins execution and application delegate receives the notifications.
- **Appdelegate** is the application delegate object which inherits from UIResponder and implements the UIApplicationDelegate protocol.
- Main entry point of the app is UIApplicationDelegate which is a protocol to be implemented in our app to get notified to app launch and user events

##Execution stages for a App:
There are mainly Five stages in App lifecycle,
- **Not Running state** : The app has not been launched or terminated by the system.
- **Inactive state** : The app is entering the foreground state but not receiving events.
- **Active state**: The app enters the foreground state and can process event.
- **Background state** : In this state, if there is executable code, it will execute and if there is no executable code or the execution is complete, the application will be suspended immediately.
- **Suspended state** : The app is in the background(in memory) but is not executing code and if system does not have enough memory, it will terminate the app.

##Mapping the App states with Code:
```swift
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch. 
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }
```
##The Main Run Loop:
- An app’s main run loop processes all user-related events.
- App delegate sets up the main run loop at launch time and uses it to process events and handle updates to view-based interfaces.
- main run loop executes on the app’s main thread
- main thread is serial thread and this ensures that user-related events are processed serially in the order in which they were received.