UIApplication in-depth research

Source: Internet
Author: User

UIApplication in-depth research

Many times, we do not need to care about this class, we rarely inherit this class, occasionally call this class API to implement some functions, but admittedly, this class is an important concept in iOS programming, so I write this article here to summarize the following information about this class, if the wrong place, please leave a message, thank you.

The core role of UIApplication is to provide control and collaboration during the operation of the iOS program.

Each program must have an instance of UIApplication (or its subclasses) at run time. Recall my previous article "main function study" in the article mentioned in the main function code, you can see, when the program starts to run, the Uiapplicationmain function is the program entry point, this function does a lot of work, One of the important tasks is to create a single instance of UIApplication. In your code you can get a pointer to this singleton instance by calling [UIApplication Sharedapplication].

One of the main tasks of uiapplication is to handle user events, which will queue up all user events, handle them one at a time, and, when processed, send the current event to a suitable target control for handling the event. In addition, the UIApplication instance maintains a window list (UIWindow instance) opened in the app so that it can touch any of the UIView objects in the app. The UIApplication instance is given a proxy object to handle the application's life-cycle events (such as program startup and shutdown), system events (such as incoming calls, warning alerts), and so on.

Create a new iOS app project of any type, join us at class prefix input is TC, we can see that the project generates a class:

Tcappdelegate:uiresponder <UIApplicationDelegate>

The base class for this class is Uiresponder, and the project that was generated before 4.2 is different, previously inherited from NSObject. However, this class implements an interface called Uiapplicationdelegate, which indicates that this class is the proxy class for uiapplication instances in this project.

In the main function,

@autoreleasepool {

Returnuiapplicationmain (argc, argv, Nil,nsstringfromclass ([Tcappdelegateclass]));

}

The proxy class is passed into the Uiapplicationmain function, and the Uiapplicationmain function can tell the singleton object The instance pointer of the proxy class when it generates a unique uiapplication.

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

Applications in the iphone are vulnerable to interruptions, such as an incoming call that may cause the application to lose focus, and if the phone is answered at this time, the application will go to the background. 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, and after UIApplication receives the event it passes it to The Applicationdidreceivememorywarning () method of the delegate class, in which the delegate class can release memory operations to prevent the operating system from forcing the application to terminate.

Here are some of the features of this class:

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 state of the program's operation and introduce it after 2.0

/*

Uiapplicationstateactive,

Uiapplicationstateinactive,

Uiapplicationstatebackground

*/

if ([Uiapplicationsharedapplication].applicationstate ==uiapplicationstateinactive) {

NSLog (@ "program in Operation State");

}

4. Prevent the screen from darkening into hibernation

Prevent screen from darkening, use cautiously, default to No 2.0

[Uiapplicationsharedapplication].idletimerdisabled =yes;

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

5. Show Networking Status

Show Networking Tags 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"]];

UIApplication in-depth research

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.