iOS application life cycle

1. AppDelegate detailed introduction:

Every iPhone application has a UIApplication. UIApplication is the beginning of the iPhone application and is responsible for initializing and displaying the UIWindow, and is responsible for loading the application's first UIView into the UIWindow form. Another task of UIApplication is to help manage the life cycle of the application, and UIApplication performs this task through a proxy class named UIApplicationDelegate. Although UIApplication is responsible for receiving events, UIApplicationDelegate determines how the application responds to these events. The events that UIApplicationDelegate can handle include application life cycle events (such as program startup and shutdown), system events (such as incoming calls, note warnings), This article will introduce how to load the application's UIView into UIWindow and how to use UIApplicationDelegate to handle system events.

  • Before iOS13, AppDelegate was solely responsible for handling the App life cycle and UI life cycle;
  • After iOS13, AppDelegate's responsibilities are:
  1. Handle App life cycle;
  2. The new Scene Session life cycle, the UI life cycle is handed over to the new Scene Delegate.

2. Introduction to the application life cycle:

1. Application status:
  • Not running: Not running, the program has not started.

  • Inactive: Inactive, the program is running in the foreground, but does not receive any events, or is forcibly occupied by other tasks, such as a sudden call, the phone automatically locks the screen when the program is in the foreground, etc. The program usually stays in this state without event handling.

  • Active: Activated, the program runs in the foreground and receives events. This is also a normal mode for the front desk.

  • Backgroud: The program is in the background but can execute code. After most programs enter the background state, they will only stay in the background state for a period of time. When the time is up, they will enter the suspended state (Suspended). Some programs can stay in the Backgroud state for a long time without entering the suspended state (Suspended) after special requests.

  • Suspended: Suspended, the program cannot execute code in the background. The system will automatically change the background program to this state without issuing a notification. When suspended, the program still resides in memory. When the system memory is insufficient, the system will clear hung programs to provide more memory for foreground programs.

    [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-ChHg12ht-1641890651119)(/Users/yichen/Library/Application Support/typora-user-images/image-20211224134019851 .png)]

2. Some methods in AppDelegate

(1)willFinishLaunching

//willFinishLaunching:程序将要完成启动。只在程序启动时执行一次。launchOptions:存存储程序启动的原因。
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}

(2) didFinishLaunching:

didFinishLaunching is the last state executed before the app displays the interface to the user. Of course, we can complete some basic initialization in this method, but it should be a lightweight initialization operation, otherwise the program startup time will be long.

//didFinishLaunching是在app显示界面给用户之前最后执行的状态,理所当然我们可以在这个方法内完成一些基本的初始化,但是应该是轻量级的初始化操作,不然会导致程序启动时间长,影响用户体验。
//例如:
//不使用StoryBoard,需要配置window
//第三方登录sdk注册,以及很多第三方sdk的初始化配置
//判断程序接下来的业务逻辑,播不播放引导图片?播不播放开屏广告?有没有登录?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //若用户直接启动,launchOptions内无数据。
 
    //若由其他程序通过openURL:启动。
    //launchOptions中UIApplicationLaunchOptionsURLKey的数据为openURL:的参数url。
    //launchOptions中UIApplicationLaunchOptionsSourceApplicationKey的数据为启动本程序的源程序的bundle ID(NSString)。
    NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
    if (url) {
        [self launchOptionsWithURL:url];
    }
    
    NSString *bundleId = [launchOptions objectForKey:UIApplicationLaunchOptionsSourceApplicationKey];
    if (bundleId) {
        [self launchOptionsWithBundleId:bundleId];
    }
    //若由远程通知启动。
    //launchOptions中UIApplicationLaunchOptionsRemoteNotificationKey对应数据为启动本程序的远程通知信息useinfo(NSDictionary)。
    NSDictionary *useinfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (useinfo) {
        [self launchOptionsWithUseinfo:useinfo];
    }
    
    return YES;
}

(3) DidBecomeActive:

DidBecomeActive : The program has been activated. Called when the program is in the foreground: when the program is started for the first time and when it returns to the foreground from the background.

If the program was in the background before, you can re-fetch the data and refresh the user interface in this method.

//程序第一次启动,从后台到前台时调用。
- (void)applicationDidBecomeActive:(UIApplication *)application {
    //程序进入前台时,记录当前时间
    
}

(4) WillResignActive:

WillResignActive : The program is about to become inactive. The program is called when it is in the foreground: there is an incoming call, the phone is locked, etc. If the program is forcibly occupied by other tasks, this method will be called. This method is also called the interrupt method.

- (void)applicationWillResignActive:(UIApplication *)application{
    //程序准备进入后台通知相关VC 记录当前时间
}

(5) DidEnterBackground

The function is:

  • Release shared resources
  • Save user data (write to hard disk)
  • void timer
  • Save enough program state to restore next time
//程序已经进入后台时调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
	//
}

**(6)WillEnterForeground **:

WillEnterForeground : The program will enter the foreground. Called when the program enters the foreground from the background. WillEnterForeground is mainly used to undo operations done in WillResignActive .

//程序将要进入前台。程序从后台进入前台时调用。
-(void)applicationWillEnterForeground:(UIApplication *)application {

}

(7)WillTerminate:

WillTerminate : The program will terminate. Called before program termination.

//目的就是为了保留一些重要的数据,方便下次启动后的恢复,达到一种让用户感觉该程序永远运行在后台从来没被终止过的感觉
- (void)applicationWillTerminate:(UIApplication *)application{
  
}
3. Time period and sequence of interaction

starting program:

  • willFinishLaunchingWithOptions : Called only once when the program starts.
  • didFinishLaunchingWithOptions : The program has completed starting and the interface will be displayed. Executed only once when the program starts.
  • DidBecomeActive : Called when the program is in the foreground: when the program is started for the first time and when it returns to the foreground from the background.

The front desk enters the background :

  • WillResignActive : The program is about to become inactive. The program is called when it is in the foreground: there is an incoming call, the phone is locked, etc. This method will be called when the program is forcibly occupied by other tasks, so this method is also called the program interrupt calling method.
  • DidEnterBackground : The program has entered the background. Called when the program enters the background from the foreground.

Enter the front desk from the background :

  • WillEnterForeground : The program will enter the foreground. Called when the program enters the foreground from the background.
  • DidBecomeActive : Called when returning from the background to the foreground.

Terminate the program :

  • WillTerminate : The program will terminate. Called before program termination.

Guess you like

Origin blog.csdn.net/weixin_42357849/article/details/122436270
Recommended