The life cycle of an iOS program

Source: Internet
Author: User
Tags uikit

The life cycle of any program refers to the time when the program is loaded to the end of the program.

In an iOS app, you can start a program by clicking the icon on the home page. When clicked, the system displays a transition interface and then calls the main () function to load the program. From this moment on, a lot of initialization work is given to Uikit, which is responsible for loading the program's user interface and initiating the event loop. During the event loop, Uikit is associated with the incoming event and custom object, and responds to the program's command events. If a user's action causes the program to exit, Uikit notifies the program and begins the process of closing the program.

In iOS programs, the main () function is seldom used, and the vast majority of the actual work is given to the Uiapplicationmain () function for processing. Therefore, when we use a template to create a new project in Xcode, the content of the main () function generated is almost identical.

 1   #import  <uikit/uikit.h>< Span style= "color: #008080;" >2  int  Main (int  argc, char  *argv[])  3  { 4  nsautoreleasepool *pool = [[ NSAutoreleasePool alloc] init];  5   Uiapplicationmain (argc, argv, nil, nil);  6   [pool release];  7  return      RetVal;  8 } 

The main () function accomplishes the following 3 tasks:

(1) Create an automatic release pool ;

(2) Call the Uiapplicationmain () function;

(3) Release the auto-release pool .

In general, developers do not need to modify the main () function.

The function Uiapplicationmain () is the core of the initialization program, it accepts 4 parameters, and it is recommended that developers never modify the 4 parameters passed in. Of these, argc and argv two parameters from the main () function parameters, and the other two string type parameters represent the main class of the program (Principal class) and the proxy class (delegate). If the primary class is nil, the default is UIApplication, and if the proxy class is nil, the program's proxy is assumed to be from the main nib file. If either parameter is not nil, then the Uiapplicationmain () function creates the corresponding function class based on the parameter. Therefore, if you use a subclass of a custom uiapplication class in your program (it is not recommended to inherit the UIApplication class to establish a custom subclass), you need to pass in your custom class name as the 3rd parameter.

Each iOS application contains an UIApplication object that the iOS system uses to monitor the entire application life cycle. Each application specifies a proxy object for the UIApplication object and is monitored to the application life cycle events by the proxy object handling UIApplication object. As a proxy class for uiapplication, the Uiapplicationdelegate protocol must be implemented first, and the protocol clearly clarifies what the agent should do or can do. The UIApplication object is responsible for listening to the application's lifecycle events and handing the events to the proxy object for processing. Generally speaking, an iOS app has the following 5 statuses:

(1) Not running

The application is not started or the app is running but is stopped by the system on the way.

(2) Inactive

The current application is running in the foreground, but does not receive events (other code may be executing at the moment). Each time the app wants to switch from one state to another, the midway transition stays in this state briefly. The only time to stay longer is when the user locks the screen, or the user is prompted to respond to certain events (such as incoming calls, unread text messages, etc.).

(3) Active

The current app is running in the foreground and receiving events. This is the normal state at which the app is running in the foreground.

(4) Background

The application is in the background and is still executing code. Most applications that will enter the suspended state will briefly enter this state. However, applications that require additional execution time will remain in this state for a longer period of time. In addition, if an application needs to run directly into the background when it starts, such an application will enter the background state directly from the not running state and will not go through the inactive state (such as an application without an interface). Note: This is not specifically a no interface application, even if there is an interface, if you want to go directly into the background state, the application interface will not be displayed.

(5) Suspended

The application is in the background and has stopped executing code. The system automatically moves the app into this state and does not notify the app before the move. When in this state, the app still resides in memory but does not execute any program code. When the system has a low memory alarm, the system will clear the memory in the suspended state to provide enough memory for the app that is running in the foreground.

Reference: IOS 8 Development Guide, Guan Lei People's post and telecommunications press.

The life cycle of an iOS program

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.