2. The life cycle of the iOS program

Source: Internet
Author: User

Program Start-Life cycle

From: qq:853740091

1. first explain the UIApplication object

(1) The UIApplication object is a symbol of the application, and a UIApplication object represents an application.

(2) Each application has its own UIApplication object, and is a singleton, if you try to create a new UIApplication object in the program, then the error prompt.

(3) This singleton object can be obtained by [Uiapplicationsharedapplication]

(4) An iOS program is created after the first object is the UIApplication object, and only one (through the code to get two UIApplication objects, the print address can see the address is the same).

(5) Use the UIApplication object to perform some application-level operations

1.1 Application-level operation examples:

1) Set the red reminder number in the upper right corner of the application icon (e.g. when QQ message)

2) Add Daisy when setting up the app network

3) Management status bar is the app's top signal, etc.

4) OpenURL: The most common click of a button to jump to the app Stroe download interface, as well as texting, email, etc.

The UIApplication object constantly detects the current state of the application. When the system modifies the current state of the application, a notification is sent, and the UIApplication object detects the notification.

2. uiapplication Delegate

2.1. Brief description

All mobile operating systems have a fatal disadvantage: apps are vulnerable to interruptions. For example, a call or lock screen will cause the app to go backstage or even be terminated. There are a number of other similar situations that can cause the app to be disturbed, causing some system events when the app is disturbed, and UIApplication notifies its delegate object, allowing delegate agents to handle these system events.

Each new project, there is a "appdelegate" word of the class, it is UIApplication's proxy compliance Agreement <UIApplicationDelegate>

2.2 Proxy methods

Called when the application is finished (automatically called by the system)
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
NSLog (@ "didfinishlaunchingwithoptions");
return YES;
}

Called when it is about to lose its active state (loses focus, cannot interact)
-(void) Applicationwillresignactive: (uiapplication *) application
{
NSLog (@ "resignactive");
}

Regain focus (ability to interact with users)
-(void) Applicationdidbecomeactive: (uiapplication *) application
{
NSLog (@ "becomeactive");
}

Called when the application enters the background
The application's data is typically stored in this method, as well as the state
-(void) Applicationdidenterbackground: (uiapplication *) application
{
NSLog (@ "Background");
}

Called when the application is about to enter the foreground
Typically restores the application's data in this method, as well as the state
-(void) Applicationwillenterforeground: (uiapplication *) application
{
NSLog (@ "Foreground");
}

This method is called when the application is about to be destroyed
Note: This method cannot be called if the application is in a suspended state
-(void) Applicationwillterminate: (uiapplication *) application
{
}

When the application receives a memory warning, it calls the
It is common to release unwanted memory in this method
-(void) applicationdidreceivememorywarning: (uiapplication *) application
{
NSLog (@ "memorywarning");
}

3. Program Start principle

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

@autoreleasepool {

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

}

}

int argc, char * argv[] These two parameters contain information such as when the application is started (parameters passed in by the system)

@autoreleasepool cache Pool, object reference count when the automatic processor is reborn into a run loop, the system automatically creates a NSAutoreleasePool, and the NSAutoreleasePool cannot be deleted

The third parameter: mainly detects the state of the program (meaning that the system changes the current state of the program). You must pass in the UIApplication subclass if the incoming nil system will automatically create a UIApplication object

Fourth parameter: An agent for the app. The third parameter detects a change in the state of the program and notifies the agent. Then we deal with things in the appropriate way.

Note: The main function will not return when the program exits. Otherwise one is in a dead loop state

4. The complete process of program initiation

1). Main function

2). Uiapplicationmain

* Create UIApplication objects

* Create a UIApplication delegate object

3). Delegate object start processing (listen) system events (no storyboard)

* When the program is started, the agent's application:didfinishlaunchingwithoptions is called: method

* Create UIWindow in application:didfinishlaunchingwithoptions:

* Create and set UIWindow Rootviewcontroller

* Display window

4). According to Info.plist get the most important storyboard file name, load the main storyboard (with storyboard)

* Create UIWindow

* Create and set UIWindow Rootviewcontroller

* Display window

2. The life cycle of the 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.