Ios5 programming-deep research on uiapplication

Source: Internet
Author: User

In many cases, we do not need to care about this class. We seldom inherit this class and occasionally call the APIs of this class to implement some functions, but it is undeniable that, this class is a very important concept in IOS programming. So I will write this article here to summarize the information of this class. If something is wrong, leave a message. Thank you.

The core function of uiapplication is to provide control and collaborative work during IOS program running.

Each program must have only one uiapplication (or its subclass) instance at runtime. Recall the code of the main function mentioned in my previous article "Main Function Research". We can see that when the program starts to run, the uiapplicationmain function is the entry point of the program, this function has done a lot of work. One of the important tasks is to create a single instance of uiapplication. In your code, you can call [uiapplication
Sharedapplication] To get the pointer of this Singleton instance.

One of the main tasks of uiapplication is to process user events. It starts a queue, puts all user events in the queue, and processes them one by one. during processing, it sends the current event to a target control suitable for processing the event. In addition, the uiapplication instance maintains a window list (uiwindow instance) opened in this application so that it can access any uiview object in the application. The uiapplication instance is assigned a proxy object to process application lifecycle events (such as program startup and shutdown), system events (such as incoming calls, and alarm items), and so on.

Create an iOS application project of any type. Add the input in class prefix to TC. We can see that the project generates a class:

Tcappdelegate: uiresponder <uiapplicationdelegate>

The base class of this class is uiresponder, which is different from the project generated before 4.2. It was previously inherited from nsobject. In any case, this class implements an interface called uiapplicationdelegate, which indicates that this class is the proxy class of the uiapplication instance in this project.

In the main function,

@ Autoreleasepool {

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

}

The proxy class is passed in to the uiapplicationmain function. When the uiapplicationmain function generates a unique uiapplication, it can tell this singleton object the instance pointer of the proxy class.

You can use the following code to obtain the uiapplication proxy object:

 

Tcappdelegate * mydelegate = (tcappdelegate *) [uiapplicationsharedapplication]. Delegate;

 

When uiapplication receives system events and lifecycle events, it passes the corresponding events to uiapplicationdelegate for processing. The lifecycle functions listed in the following table are mostly optional, but for the robustness of applications, programmers should implement them.

Uiapplicationdelegate event-Handling Methods

Method Signature

1: Application: didchangestatusbarframe:

-(Void) Application :( uiapplication *) Application didchangesetstatusbarframe :( cgrect) oldstatusbarframe

2: Application: didchangestatusbarorientation:

-(Void) Application :( uiapplication *) Application didchangestatusbarorientation :( uiinterfaceorientation) oldstatusbarorientation

3: Application: handleopenurl:

-(Bool) Application :( uiapplication *) Application handleopenurl :( nsurl *) URL

4: Application: willchangestatusbarorientation: Duration:

-(Void) Application :( uiapplication *) Application willchangestatusbarorientation:

(Uiinterfaceorientation) newstatusbarorientation
Duration :( nstimeinterval) Duration

5: Application: willchangestatusbarframe

-(Void) Application :( uiapplication) Application willchangestatusbarframe :( cgrect) newstatusbarframe

6: applicationdidbecomeactive:

-(Void) applicationdidbecomeactive :( uiapplication *) Application

7: applicationdidfinishlaunching:

-(Void) applicationdidfinishlaunching :( uiapplication *) Application

8: applicationdidreceivememorywarning:

-(Void) applicationdidreceivememorywarning :( uiapplication *) Application

9: applicationsignificanttimechange:

-(Void) applicationsignificanttimechange :( uiapplication *) Application

10: applicationwillresignactive:

-(Void) applicationwillresignactive :( uiapplication *) Application

11: applicationwillterminate:

-(Void) applicationwillterminate :( uiapplication *) Application

 

1.-(void) applicationwillresignactive :( uiapplication *) Application

Note: When an application is about to run in an inactive state, during this period, the application does not receive messages or events, such as calls.

2.-(void) applicationdidbecomeactive :( uiapplication *) Application

Note: When the application enters the activity status for execution, this is exactly the opposite of the method above.

3.-(void) applicationdidenterbackground :( uiapplication *) Application

Note: This API is called when a 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

Note: This method is called when the program is to return to the foreground from the background.

5.-(void) applicationwillterminate :( uiapplication *) Application

Note: A program is called to exit. It is usually used to save data and clean up before exiting. You need to set the key value of uiapplicationexitsonsuspend.

6.-(void) applicationdidreceivememorywarning :( uiapplication *) Application

Note: iPhone devices only have limited memory. If too much memory is allocated to the application, the operating system will terminate the operation of the application. This method will be executed before the termination, you can usually clean up the memory here to prevent the program from being terminated.

7.-(void) applicationsignificanttimechange :( uiapplication *) Application

Description: executed when the system time changes

8.-(void) applicationdidfinishlaunching :( uiapplication *) Application

Note: It is executed after the program is loaded.

9.-(void) Application :( uiapplication) Application willchangestatusbarframe :( cgrect) newstatusbarframe

Description: This parameter is executed when the statusbar box is about to change.

10.-(void) Application :( uiapplication *) Application willchangestatusbarorientation:

(Uiinterfaceorientation) newstatusbarorientation

Duration :( nstimeinterval) Duration

Description: This parameter is executed when the orientation of the statusbar box is about to change.

11.-(bool) Application :( uiapplication *) Application handleopenurl :( nsurl *) URL

Note: When executed through a URL

12.-(void) Application :( uiapplication *) Application didchangestatusbarorientation :( uiinterfaceorientation) oldstatusbarorientation

Note: After the statusbar box direction changes, execute

13.-(void) Application :( uiapplication *) Application didchangesetstatusbarframe :( cgrect) oldstatusbarframe

Note: After the statusbar box changes, execute

Applications in the iPhone are easily disturbed. For example, an incoming call may cause the application to lose focus. If the application receives a call at this time, the application will go to the background to run. There are many other similar events that will cause the iPhone application to lose focus. Before the application loses focus, the applicationwillresignactive () method of the delegate class will be called, the applicationdidbecomeactive () method is called when the application gets the focus again. For example, when running an application, the screen lock will call the applicationwillresignactive () method of the delegate class, And the applicationdidbecomeactive () method will be called when the screen is unlocked.

 

Another very important method is applicationdidreceivememorywarning (), because the iPhone device only has limited memory. If too much memory is allocated to the application, the operating system will terminate the application, however, before termination, the operating system will call the didreceivememorywarning () method of the delegate class to warn the application. After the uiapplication receives this event, it will pass it to the applicationdidreceivemorywarning () method of the delegate class, the delegate class can release the memory in this method to prevent the operating system from forcibly terminating the application.

The following are some functions of this class:

1. Set the number icon on the icon

// Set the number icon on the icon of the main interface, which is introduced in 2.0. The default value is 0.

[Uiapplicationsharedapplication]. applicationiconbadgenumber
= 4;

2. Set whether redo and undo operations are supported during shaking gestures.

// Shake the gesture to determine whether redo undo operations are supported.

// Introduced after 3.0, default Yes

[Uiapplicationsharedapplication]. applicationsupportsshaketoedit
= Yes;


3. Determine the program running status

// Determine the program running status, which will be introduced after 2.0

/*

Uiapplicationstateactive,

Uiapplicationstateinactive,

Uiapplicationstatebackground

*/

If ([uiapplicationsharedapplication]. applicationstate
= Uiapplicationstateinactive ){


Nslog (@ "the program is running ");

}

4. Prevent the screen from being dimmed to sleep

// Prevent the screen from being dimmed. Use it with caution. The default value is no 2.0.

[Uiapplicationsharedapplication]. idletimerdisabled
= Yes;

Use this function with caution because it consumes a lot of power.

5. display the networking status

// Display the network tag 2.0

[Uiapplicationsharedapplication]. networkactivityindicatorvisible
= Yes;


6. display an address on Map

Nsstring * addresstext = @ "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 = @ "mailto: first@example.com? Cc = second@example.com, third@example.com & subject = Hello
From California! ";

Nsstring * Body = @ "& Body = It is raining in sunny California! ";

Nsstring * Email = [nsstringstringwithformat: @ "% @", recipients, body];

Email = [emailstringbyaddingpercentescapesusingencoding: nsutf8stringencoding];

[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: email];

8. Call a number.

// Call the Google 411

[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: @ "Tel: // 8004664411"];

9. send SMS

// Text to Google SMS

[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: @ "SMS: // 466453"];

10. Open a website

// Lanuch any iPhone developers fav site

[[Uiapplicationsharedapplication] Openurl: [nsurlurlwithstring: @ "http://itunesconnect.apple.com"];

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.