In the development process we need some global objects to connect the various parts of the program, the most important of which is the uiapplication object. But in practical programming we do not deal directly with uiapplication objects, but with their agents.
UIApplication is the beginning of the iphone application and is responsible for initializing and displaying 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 the events that can be handled include the 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.
If you create a project with the Xcode template, Xcode creates a class that inherits from Uiapplicationdelegate for the programmer, but does not automatically implement optional event handlers that inherit from Uiapplicationdelegate. If the reader creates a project called "Testuiapplication", Xcode will To create TestUIApplicationAppDelegate.h and TESTUIAPPLICATIONAPPDELEGATE.M files, the file is declared as follows:
@interface Testuiapplicationappdelegate:nsobject <UIApplicationDelegate>
The application's uiapplication is defined in the Mainwindow.xib file and has a uiapplicationdelegate reference as a outlet.
The iphone is not a multitasking operating system, so the application is vulnerable to interruptions, such as a call that could cause the application to lose focus, and if the phone is answered at this time, the application will automatically terminate the operation. There are many other similar events that cause the iphone application to lose focus and call the Applicationwillresignactive () method of the delegate class before the application loses focus. The Applicationdidbecomeactive () method is called when the application gets to the focus again. For example, when running the application, the lock screen invokes the Applicationwillresignactive () method of the delegate class, and when the screen is unlocked, the Applicationdidbecomeactive () method is called.
Another very important approach is applicationdidreceivememorywarning (), because the iphone device has only limited memory, and if the application is assigned too much memory the operating system terminates the application's operation. But before terminating, the operating system warns the application by calling the Applicationdidreceivememorywarning () method of the delegate class first. After UIApplication receives this event, it passes it to the applicationdidreceivememorywarning () method of the delegate class, where the delegate class can release memory operations to prevent the operating system from forcing the application to terminate.
Now let's look at what the methods defined in the agreement are to be implemented, respectively:
1,-(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
2,-(void) Applicationdidbecomeactive: (uiapplication *) application
Description: When the application executes in the active state, this is exactly the opposite of the method above
3,-(void) Applicationdidenterbackground: (uiapplication *) application
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
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) Applicationwillterminate: (uiapplication *) application
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.
6,-(void) applicationdidreceivememorywarning: (uiapplication *) application
Description: The iphone device has only limited memory, and if the application is assigned too much memory the operating system will terminate the application's operation, this method will be executed before termination, usually can be done in the memory cleanup work to prevent the program from being terminated
7,-(void) Applicationsignificanttimechange: (uiapplication*) application
Description: Executes when the system time has changed
8,-(void) applicationdidfinishlaunching: (uiapplication*) application
Description: Executes when the program is loaded
9,-(void) application: (uiapplication) Application Willchangestatusbarframe: (cgrect) Newstatusbarframe
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
UIApplication objects and their proxies uiapplicationdelegate[]