To study the life cycle of an application, add a method to print the current function name in each method:
As follows:
1. Run the program:
Output:
2, click the Home button
3. Click Apply Again
4. Double-clickHome键, swipe up the app, kill the app
This time the console does not have any output, because you directly kill the application is not called applicationWillTerminate , only when the application is killed by the system will call this method.
5. Supplement
When you receive a push notification, you get the following output:applicationWillResignActive:
Swipe up at the notification banner that pops up so that the banner disappears, and you get the following output:applicationDidBecomeActive
Summarize:
//the life cycle method of the application. //the method that is called after the application finishes booting. //in this method we can create the controls to be displayed. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Override point for customization after application launch. //Prints the current function name and the number of rows in the file where the current code is located//Prints the current function name, nsstringfromselector the string that gets the method represented by the selector of the parameterNSLog (@"%@", Nsstringfromselector (_cmd)); //Add the controls you want to display here//to display the control on the application, you must have a window, the display of the control is directly or indirectly placed on the window to display, in general, an application has only one window. //Creating a Window objectSelf.window =[[UIWindow alloc] init]; //UIScreen Screen class Mainscreen to the current device's home screen, bounds the size of the screen to take//set the size and coordinates of a windowSelf.window.frame =[[UIScreen mainscreen] bounds]; //set the window's background color to redSelf.window.backgroundColor =[Uicolor Redcolor]; //let the window display[Self.window makekeyandvisible]; returnYES;}//background Switch to foreground: background--inactive status--foreground (active)//foreground switch to background: foreground (active)--inactive status--backstage- (void) Applicationwillresignactive: (UIApplication *) application{NSLog (@"%@", Nsstringfromselector (_cmd)); //when is the method called? //This method is called when the application is about to switch from active to inactive, which occurs when something is temporarily interrupted (such as a call, a text message, etc.) or when the user exits the application. And the application starts transitioning to the background state. //Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. //What can we do in this method? //using this method, we can pause the task that is being performed, deactivate the timer, reduce the frame rate of OpenGL ES, and the game should be paused in this method. //Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}//the method that is called after the application enters the background. - (void) Applicationdidenterbackground: (UIApplication *) Application {//what can be done in this method? //In this method, we can release the shared resource, save the user data, invalidate the timer, and save enough application state information to restore the current state of the application. //Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in Formation to the restore your application to the it is terminated later. //Configure whether an application can be in the background: Add an item to Info.plist: application does not run in background YES does not support background run//This method is called when the user exits the application, if your application supports running in the background. If your application does not support background running, Applicationwillterminate: This method is called. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits.NSLog (@"%@", Nsstringfromselector (_cmd));}//method called when the application is about to enter the foreground- (void) Applicationwillenterforeground: (UIApplication *) Application {//This method is called when it enters inactive state from the background. Here, you can undo many of the changes that have been made after entering the background. (Recover user data)NSLog (@"%@", Nsstringfromselector (_cmd));}//method called after the application has become active- (void) Applicationdidbecomeactive: (UIApplication *) Application {//In this method, restarting a task that was previously paused (or a task that has not yet started), we can choose to refresh the user interface if the application was previously run in the background. //Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.NSLog (@"%@", Nsstringfromselector (_cmd));}- (void) Applicationwillterminate: (UIApplication *) Application {//This method is called when the application is about to terminate. Save the appropriate data in this method. //Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.NSLog (@"%@", Nsstringfromselector (_cmd));}//method called when the application receives a memory warning- (void) Applicationdidreceivememorywarning: (UIApplication *) application{//releasing temporarily useless resourcesNSLog (@"%@", Nsstringfromselector (_cmd));}
The life cycle of an IOS app