iOS Application Life Cycle Example

When you create an iOS application with Xcode, the following two files play the primary role in the application development. AppDelegate.swift and ViewController.swift ( In Objective-C project the file are AppDelegate.h,AppDelegate.m,ViewController.h and ViewController.m). No matter swift or objective-c, the concept is same.

AppDelegate is the application delegate object. It inherits the UIResponder class and implements the UIApplicationDelegate delegate protocol. The UIResponder class can make AppDelegate have the ability to response events, and UIApplicationDelegate enables the AppDelegate to be an application delegate object to manage and respond to the life cycle of the application.

The ViewController class inherits from the UIViewController class and plays the role of the root view and user event controller in the project.

When you open the source code of AppDelegate class, you will find there are some methods that has been already overwrite in it. Each method will be invoked according to one state in iOS app’s life cycle.

1. iOS App Life Cycle State.

Every iOS application has five state in it’s life cycle.

ios app life cycle state relationship

  1. Not Running state : The app not started or be stoped.
  2. Inactive state : The app is entering the foreground state, but cannot process event.
  3. Active state: The app enters the foreground state and can process event.
  4. Background state : The app goes into the background, and 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.
  5. Suspended state : The suspended application goes into a frozen state, unable to execute code, and terminate if the system does not have enough memory.
READ :   How To Add Subview In iOS Programmatically

2. iOS App Life Cycle State And AppDelegate Method Mapping.

When iOS app life cycle state changing, the related method in AppDelegate class will be invoked. Below is the state and method mapping table.

  1. When the application is started and initialized below method will be invoked, and the root view controller is instantiated at this stage.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
  2. When the application enters the foreground and is active will invoke below method, usually we can restore the state of the UI (such as game state) in this method.
    func applicationDidBecomeActive(_ application: UIApplication)
    
  3. This method is called and notified when the app change state from active state to inactive state, we usually save the status data of the UI (such as game status data) in this method.
    func applicationWillResignActive(_ application: UIApplication)
  4. This method is called and notified when the application enters the background. In this method we can save user data and release file or database resources.
    func applicationDidEnterBackground(_ application: UIApplication)
  5. This application goes into the foreground, but is not yet active will invoke this method. We can restore user data in this method.
    func applicationWillEnterForeground(_ application: UIApplication)
  6. This method is called and notified when the application is terminated. We can release resources in this method and we can also save user data in it.
    func applicationWillTerminate(_ application: UIApplication)

3. iOS App Life Cycle State And AppDelegate Method Mapping Example.

In this example, we will use NSLog to print message in the console for each of above methods, so when you start the iOS app, it will print different message in the console for different app state.

//

//  AppDelegate.swift

//  StackViewExample

//

//  Created by zhao song on 2018/7/23.

//  Copyright © 2018 dev2qa.com. All rights reserved.

//

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        

        NSLog("didFinishLaunchingWithOptions");

        

        return true

    }

    func applicationWillResignActive(_ application: UIApplication) {

        

        NSLog("applicationWillResignActive");

        

    }

    func applicationDidEnterBackground(_ application: UIApplication) {

        

        NSLog("applicationDidEnterBackground");

        

    }

    func applicationWillEnterForeground(_ application: UIApplication) {

        NSLog("applicationWillEnterForeground");

    }

    func applicationDidBecomeActive(_ application: UIApplication) {

        NSLog("applicationDidBecomeActive");

    }

    func applicationWillTerminate(_ application: UIApplication) {

        NSLog("applicationWillTerminate");

    }

}

Below is the console output when you open the app, close the app, open it again and remove the app in simulator.

READ :   How To Set iOS App Icon In Xcode Project

ios app life cycle state console output message example

(Visited 279 times, 1 visits today)

2 Comments



  1. I must say that this is a wonderful plog post, and your blog in general is great.
    I really enjoyed reading this iOS Application Life Cycle Example
    post.

    A very useful, informative and valuable here!

    Keep it up!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.