Simple usage of appdelegate and uiapplication

Source: Internet
Author: User

Since contact with iOS soon, although the project can be done, but for uiapplication, appdelegate These concepts are not very familiar with, this morning to summarize, find a good blog post, I share it.

< Span style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; Background-color:rgb (255, 255, 255); " >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 that has the MAIN.M class by default, and we all know that a main () method represents an application's entry,   

int main (int argc, char * argv[]) {@autoreleasepool {return uiapplicationmain (argc, argv, Nil, NSSTRINGFROMCLA    SS ([Appdelegate class])); }}

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, 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 introduced later, default Yes
[UIApplication sharedapplication].applicationsupportsshaketoedit =yes;


3. Judging the state of the program Operation
//Judging the running state of the program, introduced after 2.0

/* uiapplicationstateactive, uiapplicationstateinactive, Uiapplicationstatebackground */if ([UIApp      Lication sharedapplication].applicationstate ==uiapplicationstateinactive) {NSLog (@ "program in Run 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
//Display Networking Tag 2.0
[UIApplication sharedapplication].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];              [[ Uiapplication sharedapplication]openurl:[nsurlurlwithstring:urltext]]; 

< Span style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14px; Background-color:rgb (255, 255, 255); " > 7. Sending an e-mail message

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]; [[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

Simple usage of appdelegate and uiapplication

Related Article

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.