iOS Series Basics 03 Explore the application life cycle
Directory:
1. Non-operational status-application launch scenario
2. Click the Home button-the app exits the scene
3. Suspending the rerun scenario
4. Memory cleanup-Application termination scenario
5. End
This article is mainly about the iOS application in the various states of the transition process, we recommend that you modify the Appdelegate.swift, in each process to add the log output code, so as to observe its changes.
As a delegate object for an application, the Appdelegate class will callback different methods at different stages of the application life cycle.
First, let's look at the different states of iOS apps and their relationships,
Here's a brief look at the status of iOS apps in 5:
| Status name |
Status explanation |
Status description |
| Not running |
Inactive state |
The app is not running or terminated by the system. |
| Inactive |
Front-Desk Inactive status |
The app is entering the foreground state, but it cannot accept event processing. |
| Active |
Foreground activity status |
The application enters the foreground state and can accept event processing. |
| Background |
Background status |
After the app has entered the background, it can still execute the code. If there is executable code, the code executes, and if there is no executable code or the executable code is executed, the application will go into the pending state immediately. |
| Suspended |
Pending status |
Applications that are in a pending state enter a "frozen" state and cannot execute code. If the system does not have enough memory, the application is terminated. |
During the app state transitions, the iOS system will callback some of the methods in Appdelegate and send some notifications.
In fact, there are a lot of methods and notifications used in the life cycle of the app, and we've chosen several major methods and notifications for detailed introductions, such as the following table:
methods and local notifications for applying callbacks during state transitions
| Method |
Local notifications |
Description |
| Application:didfinishlaunchingwithoptions: |
Uiapplicationdidfinishlaunchingnotification |
The method is invoked and notified when the app is started and initialized. This phase instantiates the root view controller. |
| Applicationdidbecomeactive: |
Uiapplicationdidbecomeactivenotification |
The method is called and notified when the app enters the foreground and is inactive. This phase restores the state of the UI (such as game state, etc.). |
| Applicationwillresignactive: |
Uiapplicationwillresignactivenotification |
The method is called when the app enters the inactive state from the active state and is notified. This stage can save the state of the UI (such as game state, etc.). |
Applicationdidenterbackground: |
Uiapplicationdidenterbackgroundnotification |
The method is called and notified when the app enters the background. This phase can save user data, freeing up some resources (such as freeing up database resources, etc.). |
| Applicationwillenterforeground: |
Uiapplicationwillenterforegroundnotification |
The application enters the foreground, but is not yet active when the method is called and a notification is issued. This phase can restore user data. |
| Applicationwillterminate: |
Uiapplicationwillterminatenotification |
The method is called when the app is terminated, except when the memory is cleared. This phase frees up some resources and can also save user data. |
To make it easier to observe the running state of the application, we add some log output to the methods in the Appdelegate class, with the following code:
1 Import UIKit2 3 @UIApplicationMain4 classAppdelegate:uiresponder, uiapplicationdelegate {5 6var Window:uiwindow?7 8 9Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)- >Bool {TenNSLog ("%@","application:didfinishlaunchingwithoptions:") One return true A } - - func applicationwillresignactive (application:uiapplication) { theNSLog ("%@","applicationwillresignactive:") - } - - func Applicationdidenterbackground (application:uiapplication) { +NSLog ("%@","Applicationdidenterbackground:") - } + A func Applicationwillenterforeground (application:uiapplication) { atNSLog ("%@","Applicationwillenterforeground:") - } - - func applicationdidbecomeactive (application:uiapplication) { -NSLog ("%@","applicationdidbecomeactive:") - } in - func applicationwillterminate (application:uiapplication) { toNSLog ("%@","applicationwillterminate:") + } - the *}Appdelegate.swift
In order to make people more intuitively understand the relationship between the state and its corresponding methods and notifications, we use several application scenarios as the starting point to analyze the system.
PS: We strongly recommend that you modify the above-mentioned Appdelegate.swift file in conjunction with the various state transitions that will be described below, so that you can understand the changes more profoundly.
1. Non-operational status-application launch scenario
[ Scene Description:]
When the user clicks on the app icon, it may be the first time the app is launched, or it may be started again after the app terminates.
The state transition process for this scenario is, for example, a total of two stages of three states: not running-inactive-active.
- In the not running-inactive phase: Call Application:didfinishlaunchingwithoptions: Method, Issue uiapplicationdidfinishlaunchingnotification notifications.
- In inactive-active phase: Call Applicationdidbecomeactive: Method, issue uiapplicationdidbecomeactivenotification notification.
2. Click the Home button-the app exits the scene
[Scene Description:]
When the app is running (that is, the active state), clicking the Home key or having another app causes the current app to break.
The state transition process for this scenario can be divided into two situations: it can be run in the background or suspended, not in the background or suspended.
Depending on the correlation property in the product properties file (such as info.plist) application does not the run in background value, you can control both of these states:
If you open the Info.plist file with a text editor, the corresponding key for this setting is Uiapplicationexitsonsuspend.
2.1. First case of State transitions
Apps can run or hang in the background.
The state transition process for this scenario is, for example, a total of three stages with four states: active-inactive-background-suspended:
-
- In the active-inactive phase, call the Applicationwillresignactive: method to issue a uiapplicationwillregisnactivenotification notification.
- During the Inactive-background phase, the app goes from inactive to backstage (without involving the methods and notifications we want to focus on).
- In the background-suspended phase, call the Applicationdidenterbackground: method to issue a uiapplicationdidenterbackgroundnotification notification.
2.2. Second case of State transitions
The app can not run in the background or hang, and its status transitions are as follows.
A total of four stages three states: Active-inactive-background-suspended-not running:
- in the active-inactive phase: The app is moved from active to inactive (without involving the methods and notifications we want to focus on).
- in the Inactive-background phase: The app goes from inactive to backstage (without involving the methods and notifications we want to focus on).
- in the background-suspended phase: Call Applicationdidenterbackground: Method, Issue uiapplicationdidenterbackgroundnotification notifications.
- in the Suspended-not running phase: Call the Applicationwillterminate: method to issue a uiapplicationwillterminatenotification notification.
iOS does not support multitasking until iOS 4, and when you click the Home key, the app is rolled out and interrupted.
After iOS 4 (including iOS 4), the operating system can support multitasking, and when you click the Home button, the app will go backstage without interruption (except in the case of insufficient memory).
Application in the background can also be part of the processing work, completed after processing into a suspended state.
Description
Double-clicking the Home button also gives you quick access to the iOS multitasking bar, as shown in (left iOS 7 and right before iOS 6):
You can see apps that are running in the background or suspended, or apps that are in the signaled state.
Drag the icons up from the taskbar to remove the background apps to free up memory.
3. Suspending the rerun scenario
[Scene Description:]
The suspended state of the app runs again.
The state transition process for this scenario is, for example, a total of three stages with four states: Suspended-background-inactive-active.
- Suspended-background Stage: The app enters the background from the suspended state (not involving several methods and calls we tell).
- Background-inactive Stage: Call Applicationwillenterforeground: Method, issue uiapplicationwillenterforegroundnotification notification.
- Inactive-active Stage: Call Applicationdidbecomeactive: Method, issue uiapplicationbecomeactivenotificaction notification.
4. Memory cleanup-Application termination scenario
[Scene Description:]
The app enters a pending state when background processing is complete (this is a dormant state), and if a low memory warning is issued, the application cocktail clears the memory and terminates the operation in order to meet the memory needs of other applications.
The state transition process for this scenario is as follows:
When the memory clears, the application terminates the operation. There are two cases of memory cleanup, either the system forcing the memory to clear or the user manually clearing it from the taskbar (that is, deleting the app).
Once the memory is cleared, the last run state will not be saved if the app is running at this time, equivalent to the first run of the app.
In a scenario where the memory is cleared, the app does not call back any methods and does not issue any notifications.
5. End
It is strongly recommended that by modifying the Appdelegate.swift file to observe the process of each state transition, the understanding will be more profound when combined with the above graphs.
iOS Series Basics 03 Explore the application life cycle