IOS APP life cycle

Source: Internet
Author: User

Official documents:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ Theapplifecycle/theapplifecycle.html#//apple_ref/doc/uid/tp40007072-ch2-sw1

Basic points:

An entry function is the main function, and generally does not need to be changed;

Divergence point: Look at the main function, you can notice the addition of @autoreleasepool. In fact, if you start a sub-thread, you need to do something similar to ensure that the object placed in the auto release pool is released in a timely manner.

Two main run loop

Main Run Loop is responsible for handling user-related events. The UIApplication object starts the main run Loop at program startup, and the interface that handles events and updates the view is run on the main thread of the program.

divergence point: Nsrunloop's study and use.

[[nsrunloop currentrunloop] runmode:nsdefaultrunloopmode

beforedate: [nsdate datewithtimeintervalsincenow:5]];

Three app states (app status)

1 not running is not running: The program does not start or is terminated by the system;

2 Inactive Inactive : The program is running in the foreground, but no events are received. When the application is in this state, many times it is just going to turn to another State;

3 Active activation: The program runs in the foreground and receives events. This is also a normal mode of the foreground.

4 Background Backstage : The program is in the background and can execute code. The application enters this state mostly because it wants to enter the suspended state, and the application can request additional time to complete some background processing before moving from background to suspended state. In addition, an app being launched directly to the background enters this state instead of the inactive state.

5 Suspended hanging up: The program cannot execute code in the background. The system automatically turns the program into this state and does not give notice. When suspended, the program is still in memory, when the system memory is low, the system will remove the suspended program, to provide more memory for the foreground program.

Appdelegate received callback when the four program switches

    • application:willfinishlaunchingwithoptions:  when the application is started. Special handling is not normally required.

    • application:didfinishlaunchingwithoptions:  The app is invoked at startup, and the app has not been shown to the user, Stay in the so-called splash screen page, in this method to do some necessary initialization operation, previously did not set the main story board enter, it is necessary through the Code to Alloc window and set the root view controller. Note that this method does not take time-consuming operations, such as a large number of IO operations, because if the method has not been executed for a long time (about 30s in the test), iOS will kill the process.

    • Applicationdidbecomeactive: The application enters the active state, meaning that the app will switch to the foreground.

    • applicationwillresignactive: When an application is going to be inactive, during which time the application does not receive messages or events, such as coming to the phone

    • Applicationdidenterbackground: The application has entered the background state.

    • applicationwillenterforeground:  The application is going to enter the foreground, and at this point it has not become active.

    • Applicationwillterminate: The application will be terminated by the system, note that this method will not be called if the application is already in the suspended state. So if you want to save some data before the program is killed, you need to be careful to avoid doing data preservation in this method

In addition, there are methods

-(void) applicationdidfinishlaunching: (uiapplication *) application;

However, the SDK documentation clearly stated that it should be used-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: ( nsdictionary *) launchoptions substitution.

Five In the actual development process, we found that on iOS 7 and iOS 8, the root view controller's viewdidload and

-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) The order of execution of the methods in Launchoptions is different, and there is no explicit documentation, and may also be related to the way the root view controller is set.

The problem was that, in the Viewdidload method of the root view controller, Addobserver was used to see if a background task was done. , and this background task is started in didfinishlaunchingwithoptions. On iOS 7, Viewdidload is executed first after the root view controller is constructed, so that Addobserver executes before the background task sends the message, and the whole process is fine, but on iOS 8, the background task may have been executed. Postnotification has already been called, and the root view controller's viewdidload has not yet started to call, causing the addobserver not to be called and cause the bug.

Therefore, it is recommended not to have a strong relationship between the two, do not rely on each other's execution.

Six at Applicationdidenterbackground, if you still want to run the task for a long time, you can call the following method:

[[uiapplication sharedapplication] beginbackgroundtaskwithexpirationhandler: ^{

NSLog(@ "Startbackgroundtask Time Out");

Do something .....

}];

In addition, the execution of these callback functions should be completed and returned as soon as possible.

IOS APP life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.