An analysis of the life cycle of IOS application

Source: Internet
Author: User

To do ipone development it is necessary to know the life cycle of the iphone program, that is, to click on the application icon to start the program to the exit program, in the process of running the code in the end what happened, only the understanding of the life cycle, it is conducive to our developers to develop better applications.

When the user clicks on a picture, the program starts running, starting with the main function:

int main (int argc, char *argv[]) {

@autoreleasepool {

Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));

}

}

Main function:

Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]) This sentence means creating a UIApplication object and setting his delegate class, Here the third parameter nil means that the default is to create a UIApplication object, or it can be written as a subclass of UIApplication, immediately following the argument that means setting the UIApplication object's delegate,

Within Uiapplicationmain, a similar code is implemented

UIApplication *app = [[UIApplication alloc] init]; Uiapplicationdelegate *delegate = [[Uiapplicationdelegate alloc] init];app.delegate = delegate;

The UIApplication object is similar to the commander in chief, and the UIApplication object is similar to his, commander-in-chief just command, with something to do,
After the UIApplication object is created, it exists as a singleton until the end of the program, and can be obtained by [[UIApplication Sharedapplication]delegate] To obtain his proxy class, as in the following code

Appdelegate * Delegate = (appdelegate *) [[uiapplicationsharedapplication]delegate];

UIApplication object is created, and its delegate is set up, let's see what his delegate (his men) have done, into the APPDELEGATE.M

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary*) launchOptions

This message is similar to the ready event of jquery and tells you that everything is in place and you can start. It can also be likened to a presidential oath, and you can decide to propose the Secretary of State and the cabinet, as well as the arrangement of senior positions such as the chairman of the Federal Reserve.

Applicationwillterminate

This heralds the end of the process.

Map to the current program the President of the Kingdom, is your term of office, To do the handover. Now the iOS app can support the background to run, this method with less, unlike in the early times, when the user clicked on home, the application directly ended, will call this method, now click Home, the application goes to the background processing

-(void) Applicationwillresignactive: (uiapplication *) application

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

-(void) Applicationdidbecomeactive: (uiapplication *) application

Description: When the application goes into active execution, this is exactly the opposite of the method above

-(void) Applicationdidenterbackground: (uiapplication *) application

Description: Called when the program is pushed to the background. So to set up the background to continue to run, then in this function can be set, such as the user presses home to do other things, the program into the background can be in this function to save some data, so that the user to open the application again from the background, it is convenient to continue to play,

-(void) Applicationwillenterforeground: (uiapplication *) application

Description: Called when the program is going back to the foreground from the background, which is exactly the opposite of the method above.

After explaining some of the proxy methods above, give some examples to illustrate the order in which methods are called

Called when the user taps the icon

1.-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

2. The Applicationdidbecomeactive program is already active,

When the program is open, the user presses the home and the program enters the background call

1.applicationWillResignActive

2.applicationDidEnterBackground

When the user clicks the icon from the background to open the app again

1.applicationWillEnterForeground

2.applicationDidBecomeActive

Tips:

In a program that supports background execution, Applicationdidenterbackground: Replaces Applicationwillterminate: An event saved as a program snapshot. (now the app basically supports background execution)

An analysis of the life cycle of IOS application

Related Article

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.