Status of APP:

Not running: The program is Not started.

Inactive: the program runs in the foreground, but receives no events. Programs typically stay in this state without event handling.

Active: The program runs in the foreground and receives events. This is also a normal pattern for the foreground.

Background: A program is in the background and can execute code. Most programs enter this state and stay there for a while. When the time is up, it will enter the Suspended state. Some programs can be in the Backgroud state for a long time after special requests.

Suspended: A program is in the background but cannot execute code. The system automatically changes the program to this state without notification. When suspended, programs remain in memory, and when system memory is low, the system removes pending programs to provide more memory for foreground programs.

APP DidEnterBackground/WillEnterForeground:
  • 1. Enter the background —– DidEnterBackground

When the program into the background, called “applicationDidEnterBackground” delegate method is called:

  - (void)applicationDidEnterBackground:(UIApplication *)application {    }
Copy the code

  • 2. Access foreground —– WillEnterForeground

When the program into the background, called “applicationWillEnterForeground” delegate method is called:

  - (void)applicationWillEnterForeground:(UIApplication *)application {    }
Copy the code

APP DidBecomeActive/ResignActive:
  • 1. Suspend —– WillResignActive

Your App hangs when a call comes in or when the screen locks (pull down the status bar, double click the Home button to move the App up). And in this time, the UIApplicationDelegate delegate will receive a notice, call “applicationWillResignActive” method, can override this method to add hang before work, such as: shut down the network, and save the data.

  - (void)applicationWillResignActive:(UIApplication*)application {    }   
Copy the code

When an application is suspended, it will not run in the background.

  • 2. Restore —– DidBecomeActive

When recovery program, another called “applicationDidBecomeActive delegate method is called,” prior to this you can use the saved data before hanging to restore your application:

  - (void)applicationDidBecomeActive:(UIApplication*)application {    }
Copy the code

Note: the application at startup, after calling the “applicationDidFinishLaunching” method The same __ will __ call “applicationDidBecomeActive” method!!!!!! Make sure your code separates recovery from startup to avoid logic bugs.

The termination of the APP

The program terminates when the user presses the button or shuts down. When a program is about to terminate normally, the “applicationWillTerminate” method is called. ⚠️ : But this method is not called if the main button is pressed to force exit. This method is used to perform the rest of the cleanup ~ for example, all connections are closed normally and any other necessary work is performed before the program exits:

  - (void)applicationWillTerminate:(UIApplication*)application {    }
Copy the code

Proxy method for APP state changes:
/ / program starts running - (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (launchOptions NSDictionary *) { // Override point for customization after application launch. return YES; } / / program hang - (void) applicationWillResignActive application: (UIApplication *) {/ * Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. */ /* Use this method to pause ongoing tasks, disable timers, And invalidate graphics rendering callbacks. Games should use this method to pause the game (void)applicationDidEnterBackground:(UIApplication *)application { /* Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated  later. */ /* If your application supports background execution, this method is called instead of applicationWillTerminate: When the user quits. * /} / / program into the front desk - (void) applicationWillEnterForeground application: (UIApplication *) {/ * Called the as part of the transition from the background to the active state; Here you can undo many of the changes made on entering the background. */ (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, Optionally refresh the user interface. * / / /} - (void) program terminates applicationWillTerminate: (UIApplication *) application {/ * Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. */ }Copy the code

applicationOperation processChange of stateThe corresponding “UIApplicationDelegate” method executes:
  • First run:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    - (void)applicationDidBecomeActive:(UIApplication *)application
    Copy the code
  • First close (home) :

    - (void)applicationWillResignActive:(UIApplication *)application
    - (void)applicationDidEnterBackground:(UIApplication *)application
    Copy the code
  • Run again:

    - (void)applicationWillEnterForeground:(UIApplication *)application
    - (void)applicationDidBecomeActive:(UIApplication *)application
    Copy the code
  • Closed again:

    - (void)applicationWillResignActive:(UIApplication *)application
    - (void)applicationDidEnterBackground:(UIApplication *)application
    Copy the code

Advanced:

  • “Click the desktop icon to start the App normally” or “click the push message after killing the process to start the App” : 1.application:willFinishLaunchingWithOptions 2.application:application:didFinishLaunchingWithOptions 3. ApplicationDidBecomeActive 4. Application: didRegisterForRemoteNotificationsWithDeviceToken / / receiving and processing messages
  • Drag down the notification center/double click the Home button to makeApp screen moves up: applicationWillResignActive
  • Drag the notification center/and double-click the Home button to makeApp interface __ returns to original __: applicationDidBecomeActive
  • Press the Home button to enableApp Into the background 1.applicationWillResignActive 2.applicationDidEnterBackground
  • Click the App icon to makeThe App from the backgroundRestore to foreground 1.applicationWillEnterForeground 2.applicationDidBecomeActive
  • Click in the notification centerRemote pushTo makeThe App from the backgroundInto the front deskApplicationWillEnterForeground 2. Application: didReceiveRemoteNotification / / 3. Receiving, processing notifications applicationDidBecomeActive
  • Swipe up or hold the App icon and select the minus icon to kill applicationWillTerminate
  • After switching from an APP to another APP like wechat, When switching back 1. ApplicationWillEnterForeground 2. Application: openURL: sourceApplication / / application between ⭐ ️ by value ⭐ ️ 3.applicationDidBecomeActive

UIApplicationDelegate provides a summary of the order in which some of the most commonly used functions are executed

Finally, I recommend a few more intuitive images:

The Main Run Loop ————— The Main Run Loop is responsible for handling user-related events. The UIApplication object launches the Main Run Loop when the program starts, which handles events and updates the view’s interface. The Main Run Loop runs on the Main thread of the program. This ensures that events that receive user-related actions are processed sequentially.

The user operates the device, and the related operation events are generated by the system and distributed through the specified port of UIKit. Events are queued internally and sent one by one to the Main Run Loop for processing. The UIApplication object is the first object to receive the time, and it determines how the event will be handled. The touch event is distributed to the main window, which is then distributed to the View that triggers the touch event. Other events are distributed to other object variables for processing in other ways.

Most of these events can be distributed within your app, such as touch events, remote control events (such as a live-by-wire headset) are handled by the App’s Responder Objects object. Responder Objects are all over your app, UIApplication objects, View objects, View Controller objects, all resopNder Objects. The target of most events is specified as a ResopNder Object, but events can also be passed to other objects. For example, if the View object does not handle events, it can be passed to the parent View or view Controller.

Source of images and content: Application life cycle

(2017.01.19)

goyohol’s essay