Lifecycle of iOS applications

Posted by reflash on Fri, 28 Jun 2019 02:40:52 +0200

Whether it's learning object C or other languages, the first thing we need to know is how the language works in the program and how the life cycle is. Learning about iOS mobile phone development, we need to understand the life cycle of iOS program. Now let's understand the life cycle of iOS program iOS.

The entry program of iOS program is the same as that of other languages, whether it starts from mian function or from mian function. The main.m file in the following figure is the start entry of the program.

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

@ autorelease pool {} This is an automatic release pool that releases memory after the program has finished.
1. The argc and argv parameters are designed to be consistent with the C language. They are not used here. They are not detailed.
2. The latter two parameters are principalClassName (main class name) and delegateClassName (delegate class name).

UI Application Main function, the first two are the same as main function, the emphasis is on the latter two. Official explanation is that the latter two parameters represent the principal class and the delegate class of the program respectively.
(1) If principalClassName is nil, its value will be obtained from Info.plist, and if it is not in Info.plist, it will default to UIApplication. The principalClass class does nothing but manage the entire life cycle of the program. It only listens for events and then gives them to delegateClass to do.
(2)delegateClass will instantiate an object NSStringFromClass([AppDelegate class]) // equivalent to @"AppDelegate" when the project is newly built.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"didFinishLaunchingWithOptions");
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    NSLog(@"applicationWillResignActive");
}

- (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.
    NSLog(@"applicationDidEnterBackground");
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    NSLog(@"applicationWillEnterForeground");
}

- (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.
    NSLog(@"applicationDidBecomeActive");
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"applicationWillTerminate");
}

1. Application didFinish Launching WithOptions: When the application is started, the application starts the entry and executes only once when the application is started. If the user starts directly, there is no data in lauchOptions. If the application is started by other means, lauchOptions contains the corresponding content.
2. Application WillResignActive: Delegate calls to be performed when an application is about to switch from active to inactive state, such as pressing the home button, returning to the home screen, or switching applications between full screens.
3. Application DidEnterBackground: Delegate call to be executed when the application has entered the daemon.
4. Application WillEnter Foreground: When the application is about to enter the foreground (activated), the delegate call to be executed corresponds to the application WillResignActive method.
5. Application Did BecomeActive: The delegate call to be executed after the application has been activated corresponds to the application DidEnterBackground method.
6. Application WillTerminate: To execute a delegate call when the application is ready for full roll-out, you need to set the key value of UI Application ExitsOnSuspend

Given the printing below, we can see the sequence of their interactions.

Start the program

2017-05-26 15:20:55.617 05-Runtime [96388:5466613] DidFinish Launching WithOptions
2017-05-26 15:20:56.046 05-Runtime [96388:5466613] Application Did Become Active

Press home to go back to the background

2017-05-26 15:21:10.642 05-Runtime [96388:5466613] Application WillResign Active
2017-05-26 15:21:11.366 05-Runtime [96388:5466613] Application Did Enter Background

When you re-click into the program

2017-05-26 15:21:15.462 05-Runtime [96388:5466613] Application Will Enter Foreground
2017-05-26 15:21:15.910 05-Runtime [96388:5466613] Application Did Become Active

This is the life cycle of iOS program, beginners should learn simply and deepen the basic understanding.

Topics: iOS Mobile C