Appdelegate details of the iOS app life cycle

Source: Internet
Author: User

The appdelegate.m/h file in IOS is important because it manages the entire life cycle of application.

First understand that each iphone app has a uiapplication,uiapplication is the beginning of the iphone app and is responsible for initializing and displaying the UIWindow, and is responsible for loading the first uiview of the application into the UIWindow form. Another task of uiapplication is to help manage the life cycle of the application, and uiapplication to perform this task through a proxy class named Uiapplicationdelegate. Although UIApplication is responsible for receiving events, Uiapplicationdelegate determines how the application responds to these events, Uiapplicationdelegate can handle Events include application lifecycle events (such as program startup and shutdown), system events (such as incoming calls, warning alerts), this article describes how to load the application's UIView to UIWindow and how to handle system events using Uiapplicationdelegate.

It is usually not necessary for UIApplication readers to modify it, only to know uiapplication receive system events, and how to write code to handle these system events is the work of the programmer. Handling system events requires writing a class that inherits from the Uiapplicationdelegate interface, while the Uiapplicationdelegate interface provides life cycle functions to handle the application and system events of the application, as shown in the following table:

1,-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
NSLog (@ "Execute when program is loaded");
}
Description: When the program is loaded and executed, the application starts the portal. Executes only once when the application starts. That is, the delegate call to execute after the application starts. The application parameter is used to obtain the state, variable, etc. of the application, and it is worth noting the dictionary parameter: (nsdictionary *) launchoptions, which stores the reason for the program to start.

If the user launches directly, no data in the lauchoptions;

If it is started by another application through OpenURL: Uiapplicationlaunchoptionsurlkey, the corresponding object is the startup URL (Nsurl), Uiapplicationlaunchoptionssourceapplicationkey the bundle ID of the source application that corresponds to the startup (NSString);

If initiated by a local notification, the Uiapplicationlaunchoptionslocalnotificationkey corresponds to the local notification object (uilocalnotification) for the startup application;

If initiated by a remote notification, the Uiapplicationlaunchoptionsremotenotificationkey corresponds to the remote notification information userinfo (nsdictionary) that initiates the application;

Other keys also have Uiapplicationlaunchoptionsannotationkey,uiapplicationlaunchoptionslocationkey,
Uiapplicationlaunchoptionsnewsstanddownloadskey.

If you want to make some distinctions at startup, you need to do something in the following code。 For example, an app can be picked up by some other app (as a sub-app for that app), and to achieve a single sign-on, you need to make reasonable authentication in the boot code and skip the login.
Example:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Nsurl *url = [Launchoptions Objectforkey:uiapplicationlaunchoptionsurlkey];
if (URL)
{
}
NSString *bundleid = [Launchoptions Objectforkey:uiapplicationlaunchoptionssourceapplicationkey];
if (Bundleid)
{
}
Uilocalnotification * localnotify = [launchoptions Objectforkey:uiapplicationlaunchoptionslocalnotificationkey];
if (localnotify)
{
}
Nsdictionary * UserInfo = [launchoptions Objectforkey:uiapplicationlaunchoptionsremotenotificationkey];
if (userInfo)
{
}
}

2,-(void) Applicationwillresignactive: (uiapplication *) application
{
NSLog (@ "Application is going to be inactive, coming into the background");
}
When the application is going to be switched from active to inactive, the delegate calls to be performed, such as pressing the home button, returning to the home screen, or switching the application between the full screen and so on.
Description: When an application is about to go into an inactive state, during which time the application does not receive messages or events, such as a call.

3,-(void) Applicationdidenterbackground: (uiapplication *) application
{
NSLog (@ "If the application supports background running, the application is already running in the background");
}

Description: Called when the program is pushed to the background. So to set the background to continue running, you can set it in this function


4,-(void) Applicationwillenterforeground: (uiapplication *) application
{
NSLog (@ "Application will enter the active state, will enter the foreground running");
}
Description: Called when the program is going back to the foreground from the background, which is exactly the opposite of the method above.

5,-(void) Applicationdidbecomeactive: (uiapplication *) application
{
NSLog (@ "application has entered the foreground, is active");
}
Description: Executes when the application enters the active state, which is exactly the opposite of the method above.

6,-(void) Applicationwillterminate: (uiapplication *) application
{
NSLog (@ "application will exit, usually used to save data and some cleanup work before exiting");
}
Description: When the program is about to exit is called, it is usually used to save the data and some cleanup work before exiting. This needs to set the key value of the uiapplicationexitsonsuspend.

7,-(void) applicationdidreceivememorywarning: (uiapplication *) application
{
NSLog (@ "system memory is low, need to clean up");
}
Description: The iphone device has only a limited amount of memory, and if the application is assigned too much memory the operating system terminates the operation of the application and executes this method before terminating, usually where the memory cleanup is done to prevent the program from being terminated.

8,-(void) Applicationsignificanttimechange: (uiapplication *) application
{
NSLog (@ "Execute when system time changes");
}
Description: Executes when the system time has changed

9,-(void) application: (uiapplication) Application Willchangestatusbarframe: (cgrect) Newstatusbarframe
{
NSLog (@ "statusbar box will change");
}
Description: Executes when the StatusBar box is going to change

10,-(void) application: (uiapplication*) Application willchangestatusbarorientation:
(uiinterfaceorientation) newstatusbarorientation Duration: (nstimeinterval) duration
{
}
Description: Executed when the StatusBar box direction is going to change

11,-(BOOL) Application: (uiapplication*) Application Handleopenurl: (nsurl*) URL
{
}
Description: When executing via URL

12,-(void) application: (uiapplication*) Application didchangestatusbarorientation: (uiinterfaceorientation) Oldstatusbarorientation
{
}
Description: Executes when the StatusBar box direction changes

13,-(void) application: (uiapplication*) Application Didchangesetstatusbarframe: (cgrect) Oldstatusbarframe
{
}
Description: Executes when the StatusBar box changes

There are other protocol methods to know:
Handling remote notifications (for remoting messages)

-(void)Application:(uiapplication *)ApplicationDidreceiveremotenotification: (nsdictonary *) userinfo
Description: When a running application receives a remote notification sent to the delegate to go ...
-(void)Application: (UIApplication *)ApplicationDidregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken
Description: When an application succeeds in registering a push service (APS) to send to the delegate to go ...
-(void)Application:(uiapplication *)ApplicationDidfailtoregisterforremotenotificationswitherror: (Nserror *) error
Description: When APS cannot successfully complete a push to the program process to send to the delegate ...

Handling local Notification (processing native messages)

-(void)Application:(uiapplication *)ApplicationDidreceivelocalnotification: (uilocalnotification *) notification
Description: When a running application receives a local notification sent to the delegate to go ...

Responding to content protections changes (changes in response to protected content)

-applicationprotecteddatawillbecomeunavailable:
Description: A notification delegate that the protected file currently becomes unavailable
-applicationprotecteddatawillbecomeavailable:
Description: Notifies the delegate that a protected file is currently available
Excerpt from: http://blog.sina.com.cn/s/blog_bf9843bf0101kgyr.html Thanks!!!

Appdelegate details of the 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.