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 the 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 respond to 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 have been already overwritten in it. Each method will be invoked according to one state in the iOS app’s life cycle.

1. iOS App Life Cycle State.

  1. Every iOS application has five states in it’s life cycle.
    ios-app-life-cycle-state-relationship
  2. Not Running state: The app has not started or be stoped.
  3. Inactive state: The app is entering the foreground state, but cannot process events.
  4. Active state: The app enters the foreground state and can process events.
  5. 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.
  6. Suspended state: The suspended application goes into a frozen state, is unable to execute code, and terminates if the system does not have enough memory.

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

  1. When the iOS app life cycle state changing, the related method in AppDelegate class will be invoked. Below is the state and method mapping table.
  2. 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]?)
  3. When the application enters the foreground and is active will invoke the below method, usually, we can restore the state of the UI (such as the game state) in this method.
    func applicationDidBecomeActive(_ application: UIApplication)
    
  4. This method is called and notified when the app changes state from an active state to an inactive state, we usually save the status data of the UI (such as game status data) in this method.
    func applicationWillResignActive(_ application: UIApplication)
  5. 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)
  6. The application that 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)
  7. 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.

  1. In this example, we will use NSLog to print messages in the console for each of the above methods, so when you start the iOS app, it will print the different messages in the console for different app states.
    //
    
    //  AppDelegate.swift
    
    //  StackViewExample
    
    //
    
    //  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");
    
        }
    
    }
  2. Below is the console output when you open the app, close the app, open it again and remove the app in the simulator.
    Stackviewixample[5707:254915] didFinishLaunchingWithOptions
    Stackviewixample[5707:254915] app1icationDidBecomeActive
    Stackviewixample[5707:254915] applicationWillResignActive
    Stackviewixample[5707:254915] app1icationDidEnterBackground
    Stackviewixample[5707:254915] applicationWillEnterForeground
    Stackviewixample[5707:254915] app1icationDidBecomeActive
    Stackviewixample[5707:254915] applicationWillResignActive
    Stackviewixample[5707:254915] app1icationDidEnterBackground
    Stackviewixample[5707:254915] app1icationWi1lTerminate

4 thoughts on “iOS Application Life Cycle Example”

  1. You are very clear to deliver the message usually i didn’t post the comment but you know I conn’t leave your tutorial without give any remarks. I really appreciate your work and I say. “Thank you”

  2. Eden - Technolotal

    I must say that this is a wonderful blog 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!

Leave a Comment

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.