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.
- Every iOS application has five states in it’s life cycle.
- Not Running state: The app has not started or be stoped.
- Inactive state: The app is entering the foreground state, but cannot process events.
- Active state: The app enters the foreground state and can process events.
- 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.
- 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.
- 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.
- 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]?)
- 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)
- 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)
- 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)
- 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)
- 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 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"); } }
- 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
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
Swift is a more modern ios programming language, it is more easy to learn than objective c. It is object oriented.
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!