[Turn] uiapplication, Appdelegate, delegate

Source: Internet
Author: User
Tags vars

Transfer from http://blog.csdn.net/xingxing513234072/article/details/16342889

UIApplication, Appdelegate, entrustment, etc.?
What is a delegate? Why should we have a commission? What is the implementation mechanism of the Commission on the iphone?

In general, we created an iphone project, which has the MAIN.M class by default, and we all know that a main () method represents the portal of an application,
The following is the method body for the corresponding MAIN.M:

[OBJC]View Plaincopy
    1. int main (int argc, charchar *argv[])
    2. {
    3. @autoreleasepool {
    4. return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));
    5. }
    6. }


In this method body, the Uiapplicationmain () method creates an instance of the UIApplication based on the name of the Appdelegate class we provide, and the
Appdelegate as a delegate to uiapplication, generally we can get to uiapplication by class method [UIApplication Shareapplication]
a reference to;


When UIApplication receives system events and life-cycle events, the corresponding events are passed to uiapplicationdelegate for processing, and the life cycle functions listed in the following table are mostly optional,
But for the robustness of the application programmers should implement them.
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. 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.
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
[UIApplication sharedapplication].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
[UIApplication Sharedapplication].applicationsupportsshaketoedit =yes;


3. Judging the state of the program operation
Determine the state of the program's operation and introduce it after 2.0

[OBJC]View Plaincopy
    1. /*  
    2.     UIAPPLICATIONSTATEACTIVE,  
    3.     UIAPPLICATIONSTATEINACTIVE,  
    4.     */   
    5.   if ([Uiapplication sharedapplication].applicationstate ==uiapplicationstateinactive) {   
    6.        nslog (@ "program in Run State");    
    7.    }   

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
[UIApplication sharedapplication].networkactivityindicatorvisible =yes;


6. Show an address on the map

[OBJC]View Plaincopy
    1. nsstring* Addresstext =@ "1 Infinite Loop, Cupertino, CA 95014";
    2. //URL encode the spaces
    3. Addresstext = [addresstextstringbyaddingpercentescapesusingencoding:nsasciistringencoding];
    4. nsstring* Urltext = [Nsstringstringwithformat:@ "http://maps.google.com/maps?q=%@", Addresstext];
    5. [[UIApplication Sharedapplication]openurl:[nsurlurlwithstring:urltext]];

7. Send an email

[OBJC]View Plaincopy
  1. NSString *recipients =@ "Mailto:[email protected][email protected],[email Protected]&subject=hello from california! ";
  2. nsstring *body =@ "&body=it is raining in sunny california!";
  3. nsstring *email = [Nsstringstringwithformat:@ "%@%@", recipients, body];
  4. email = [Emailstringbyaddingpercentescapesusingencoding:nsutf8StringEncoding];
  5. [[UIApplication Sharedapplication]openurl:[nsurlurlwithstring:email]];

8. Call a number


Call Google 411
[[UIApplication sharedapplication]openurl:[nsurlurlwithstring:@ "tel://8004664411"];
9. Send SMS
Text to Google SMS
[[UIApplication sharedapplication]openurl:[nsurlurlwithstring:@ "sms://466453"];
10. Open a URL


Lanuch any IPhone developers fav site
[[UIApplication sharedapplication]openurl:[nsurlurlwithstring:@ "http://itunesconnect.apple.com"];




UIApplication: Generally refers to the running application, one of its main work is to handle user events, it will cause a queue, all the user events are put into the queue,
is disposed of individually, at the time of processing, it sends events to a suitable target control for handling events, and UIApplication also maintains open in this application
Window list (UIWindow instance), so that it can touch any UIView object in the application;
Appdelegate: A class that handles a particular event for another object, such as when we load a page, the delegate helps UIApplication complete
Didfinishlaunchingwithoptions action, corresponding action is executed in this method accordingly;
A delegate is a behavior that provides an opportunity for an object to react to changes in another object or to affect another object, usually consisting of 3 verbs: should, would, did

UIApplication delegate Appdelegate, then appdelegate must implement Uiapplicationdelegate protocol, this Protocol we can think of as an interface in Java,
The protocol defines a series of methods that we must implement in subclasses, and then the underlying uiapplication will automatically invoke the method we have defined, which is somewhat similar to the IOC direction control mechanism in Java
Of course, there are many places in the iphone that use a commissioned design pattern, such as this uiactionsheetdelegate, uialertviewdelegate protocol, such as we want to show in our own interface
This operation table (Uiactionsheet), then our view controller must implement this uiactionsheetdelegate protocol, rewrite some of these methods, that is, who was commissioned,
Who is going to implement the method defined in the protocol to ensure normal invocation

UIApplication Cycle-Critical steps:

Step 1, click app icon or launch the app from the app URL (such as entering the app URL in the Safari address bar).


Step 2, you will enter Uiapplicationdelegate.
-(void) applicationdidfinishlaunching: (uiapplication *) application;
Or
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions;

Step 3, if it is started from the URL, first enter the Uiapplicationdelegate
-(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl *) URL;
Then jump to step 4, otherwise jump directly to step 4;

Step 4, enter the Uiapplicationdelegate
-(void) Applicationdidbecomeactive: (uiapplication *) application;

Step 5, enter the application main loop, when the application is already active and the user can interact with the application.

Step 6, in step 5, if you press and hold the home button or perform a task switch operation, and then jump to step 8;

Step 7, in the Step 5 state, the application is interrupted (such as a call, to SMS), into the uiapplicationdelegate
-(void) Applicationwillresignactive: (uiapplication *) application;
If the user chooses not to process the remaining in the current application, then go back to step 4, and if the user chooses to process, jump to step 8;

Step 8, enter the Uiapplicationdelegate
-(void) Applicationwillterminate: (uiapplication *) application;
The current application is closed.

[Turn] uiapplication, Appdelegate, delegate

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.