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.

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 All rights reserved.


import UIKit


class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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




        return true


    func applicationWillResignActive(_ application: UIApplication) {





    func applicationDidEnterBackground(_ application: UIApplication) {





    func applicationWillEnterForeground(_ application: UIApplication) {



    func applicationDidBecomeActive(_ application: UIApplication) {



    func applicationWillTerminate(_ application: UIApplication) {




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

ios app life cycle state console output message example

0 0 votes
Article Rating
Notify of

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

Oldest Most Voted
Inline Feedbacks
View all comments
Kamran Khan
Kamran Khan

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”


what is the major difference between swift and objective C

Eden - Technolotal
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

A very useful, informative and valuable here!

Keep it up!

Would love your thoughts, please comment.x