Uiapplicationsharedapplication's explanation

Source: Internet
Author: User

The iphone application is initiated by the main function main, which is responsible for invoking the Uiapplicationmain function, as shown in the following form: int uiapplicationmain (int argc, char *argv[], NSString * Principalclassname, NSString *delegateclassname); So what exactly does the Uiapplicationmain function do? This function is mainly responsible for three things:

1) Initializes the application object from the given class name, which is an instance of initializing the UIApplication or subclass object, and if you are given nil here, then the system defaults to the UIApplication class, which is primarily the class that controls and coordinates the operation of the application. In subsequent work, you can use the static method sharedapplication to get the handle of the application.

2) Initializes an application delegate from the given application delegate class. and set the delegate to the application's delegate, here is if the passed parameter is nil, the function is called to access the Info.plist file to find the main nib file, get the application delegate.

3) Start the main event loop and start receiving events.

The above is the work of the Uiapplicationmain function, the next question is the application view display, the control of the message? Here is the responsibility of the uiapplication (or subclass) object, which mainly does the following:

1) is responsible for handling incoming user events and distributing event messages to the target object (sender, action) that should process the message. 2) Manage and control views, including rendering, control behavior, current display view, and more. 3) The object has an application delegate object that the application notifies when important events (which can include system events or life cycle control events) occur during some life cycles. For example, application startup, insufficient memory, or application completion, and so on, when these events occur, the application delegates to respond.

Through the above analysis, you can know that uiapplication is a black box to the developer, it can also be. Because all the operations can be done by its delegate, it only needs to maintain some immutable things, such as event message distribution and delivery, send an event processing request to the delegate, and so on, for example, the application load processing is complete, it sends a message to the delegate, and then the delegate can be in the Applicationdidfinishlanching in the delegate function to implement the action the developer wants. When you create an application with Xcode, an application delegate class is implemented by default. For loaded views, there is a view-related delegate class to handle the life events of the view loading process. Here are the main things that the delegate can do:  control the behavior of the application  

-(void) applicationdidfinishlaunching: (uiapplication *) application            application started.  -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions         when an application is opened due to other methods (such as URL designation or connection), the notification delegate starts  -(void) Applicationwillterminate: (uiapplication *) application          notification delegate , the application will exit at shutdown, please do some cleanup work.  -(void) applicationdidreceivememorywarning: (uiapplication *) application          notifies the delegate that the application received an out-of-memory warning from the system. -(void) Applicationsignificanttimechange: (uiapplication *) application       Notification delegate system time changes (mainly refers to the time attribute, not the specific time value)   Open url -(BOOL) Application: (UIApplication *) application Handleopenurl: ( Nsurl *) url            Open the specified url  control status bar orientation change  – Application:willChangeStatusBarOrientation:duration:         device Direction will change  –application: didchangestatusbarorientation:  Active state Change  -(void) applicationwillresignactive: (uiapplication *) application     notifies the delegated application that it will go into an inactive state during which the application does not receive messages or events. -(void) Applicationdidbecomeactive: (uiapplication *) application       Notifies the delegate application to enter Active state, please restore data  

1. Set the number icon on the icon

Set the digital icon on the main interface icon, introduced in 2.0, the default is 0 [uiapplicationsharedapplication].applicationiconbadgenumber = 4;

2. Whether the Redo,undo action is supported when setting the shake gesture

Shake gesture, whether the redo undo operation is supported. 3.0 after introduction, default YES [Uiapplicationsharedapplication].applicationsupportsshaketoedit =yes;

3. Judging the state of the program operation

Determine the program running state and introduce if ([Uiapplicationsharedapplication].applicationstate ==uiapplicationstateinactive) {NSLog after 2.0 (    @ "program in Operation State"); }

4. Prevent the screen from darkening into hibernation

Prevent the screen from darkening, use cautiously, the default is no 2.0 [uiapplicationsharedapplication].idletimerdisabled =yes;

Use this feature sparingly, because it consumes very much power.

5. Show Networking Status

Display Networking Tag 2.0 [uiapplicationsharedapplication].networkactivityindicatorvisible =yes;

6. Show an address on the map

nsstring* addresstext [email protected]"1 Infinite Loop, Cupertino, CA 95014";   URL encode the spaces addresstext = [addresstextstringbyaddingpercentescapesusingencoding:nsasciistringencoding];       nsstring* Urltext = [nsstringstringwithformat:@ "http://maps.google.com/maps?q=%@", Addresstext]; [[Uiapplicationsharedapplication]openurl:[nsurlurlwithstring:urltext]];

7. Send an email

NSString *recipients [email protected]"mailto:[email protected][email protected],[   Email protected]&subject=hello from california! ";       NSString *body [email protected]"&body=it is raining in sunny california!";    NSString *email = [nsstringstringwithformat:@ "%@%@", recipients, body];       email = [Emailstringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; [[Uiapplicationsharedapplication]openurl:[nsurlurlwithstring:email]];

8. Call a number

Call Google 411 [[uiapplicationsharedapplication]openurl:[nsurlurlwithstring:@ "tel://8004664411"]];

9. Send SMS//text to Google SMS [[uiapplicationsharedapplication]openurl:[nsurlurlwithstring:@ "sms://466453"]];

10. Open a URL

Lanuch any IPhone developers fav site [[uiapplicationsharedapplication]openurl:[nsurlurlwithstring:@]/HTTP/ Itunesconnect.apple.com "];

You can see the uiapplication header file implementation @interface Uiapplication:uiresponder <uiactionsheetdelegate>{@package id<  Uiapplicationdelegate> _delegate; This is the application delegate. Nstimer ...} Therefore, when dealing with system events in UIApplication, just go to the _delegate class to process, which is the application delegate object. We can get the object of the application delegate from the application's Singleton class object uiapplicationdelegate* mydelegate = [[UIApplication sharedapplication] delegate]; UIApplication receives all system events and lifecycle events, it passes the event to uiapplicationdelegate for processing, and for the user input event, it is passed to the corresponding target object to be processed. For example, we can invoke the Applicationwillresignactive () method of the application delegate class when the application is called, and this method is also called when the user locks the screen, and is adapted to the delegate method when the application is reopened by the user. Another common use is an out-of-memory system warning that invokes the applicationdidreceivememorywarning () method of the application's delegate class, and then we can try to free up some memory. Above is the application lifecycle (startup, Abort, recovery, exit, etc.) of the application processing uiapplication sharedapplication

This article is contributed by zhudameng213

PPT documentation may be poorly viewed on WAP side. It is recommended that you choose TXT First, or download the source file to your native view.

20120221

iOS app life cycle

The life cycle of Uiviewcontroller

The life cycle of the program

The lifecycle of a program is the whole process of launching an application to the end of an application each iOS application contains an UIApplication object that the iOS system uses to monitor the entire application lifecycle through the UIApplication object Each iOS application specifies a proxy object for its UIApplication object, and the proxy object handles the application life cycle events that the UIApplication object monitors.

iOS Apps 5 states

Not running: The app has not been started, or the app is running but is being tied on the way

System stop

Inactive: The current app is running in the foreground, but does not receive events (currently, perhaps

executing other code). Typically, a midway transition stays in this state briefly whenever the app is switching from one state to another. The only time it takes longer to stay in this state is when the user locks the screen, or the user is prompted to respond to certain events (such as phone calls, unread text messages, etc.).

Active: The current app is running in the foreground and receiving events. This is the application is in front

The normal state at which the station is running.

Background: The app is in the background and is still executing code. Most will enter

Into the suspended state, it will be briefly entered in 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. For example, there are no interface applications. Note Here does not refer to the application without the interface, in fact, it can also be an interface application, but if you want to directly into the background state, the application interface will not be displayed.

Suspended: The app is in the background and has stopped executing code. The system automatically

The app is moved into this state, and no notifications are made to the app until this is done. 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 suspended state of the application to provide enough memory for the app that is running in the foreground.

Create a UIApplication object and specify its proxy

Create a UIApplication object with the Uiapplicationmain function and specify its proxy object appdelegate; The third parameter is the subclass of the specified uiapplication to generate the UIApplication object, which is nil when the The UIApplication class initializes the default object, and the fourth parameter is the specified proxy object.

Proxy object for UIApplication

As a proxy class for uiapplication, it is necessary to implement the Uiapplicationdelegate protocol, which clearly describes what to do or can do as an agent. The UIApplication object is responsible for listening to the application's lifecycle events and handing over the lifecycle events to the UIApplication proxy object.

UIApplication Agent Object life cycle function

-(void) Applicationwillresignactive: (uiapplication *) application

Description: When the application is going to be inactive, during which time the application

Not receive messages or events, such as a call

-(void) Applicationdidbecomeactive: (uiapplication *) application

Description: When the application executes in the active state, 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 the background to continue running, you can set it in this function

UIApplication Agent Object life cycle function

-(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.

-(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 Uiapplicationexitsonsuspend

(void) Applicationdidreceivememorywarning: (uiapplic ation *) application

Description: iOS devices have limited memory, and if the application is assigned too much memory, the operating system terminates the operation of the application, which is performed before termination, usually where memory cleanup is done to prevent the program from being terminated.

UIApplication Agent Object life cycle function

(void) Applicationdidfinishlaunching: (uiapplication*) a pplication

Description: Executes when the program is loaded.

-(BOOL) Application: (uiapplication*) Application Handleopenurl: (nsurl*) URL

Description: Executes when the URL is opened.

Uiviewcontroller

Uiviewcontroller is the carrier and controller of iOS top-level view, and the interaction between user and program interface is controlled by Uiviewcontroller. Uiviewcontroller manages the life cycle of UIView and the load and release of resources.

UIView UIView and UIWindow have shown the application user interface together.

Uiviewcontroller Life Cycle Events

-(void) Loadview

Load the view resource and initialize the view

-(void) viewdidload-(void) viewdidunload

Releasing a View resource

-(void) Viewwillappear: (BOOL) animated

will be loaded out of view

-(void) Viewdidappear: (BOOL) animated

View appears

-(void) Viewwilldisappear: (BOOL) animated

The view is about to disappear

(void) Viewdiddisappear: (BOOL) animated

The view has disappeared

Uiapplicationsharedapplication's explanation

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.