iOS Development appdelegate

Source: Internet
Author: User
Tags vars home screen

After the application is created, the AppDelegate.h file and the APPDELEGATE.M file are default.

What is Appdelegate?  
Appdelegate is a proxy for the entire application, providing a similar monitoring interface for program startup, exit, and so on.




APPDELEGATE.M

the file where the interface is located, common interfaces are:

Application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions//the delegate call to execute after the application is started

Applicationwillresignactive: (uiapplication *) application//the delegate call that executes when the application is going to be switched from active to inactive, such as pressing the home button to return to the home screen, or full-screen switching between applications, etc.

Applicationdidenterbackground: (uiapplication *) application//the delegate call to execute when the application has entered the daemon. So to set the background to continue running, you can set in this function.

Applicationwillenterforeground: (uiapplication *) application//the delegate call to execute when the application is going to enter the foreground (activated) Corresponds to the Applicationwillresignactive method.

Applicationdidbecomeactive: (uiapplication *) application//the delegate call to execute after the application has been activated, exactly with the Applicationdidenterbackground Square method corresponds.

Applicationwillterminate: (uiapplication *) application//the delegate call to execute when the application is about to exit completely.



Global Variables

Appdelegate can be called throughout the application, and in other pages you can get appdelegate global variables using code snippets: Appdelegate *appdelegate=[[uiapplication Sharedapplication] delegate];

Therefore, you can define variables that need to be used globally in AppDelegate.h.



system Boot Agent (load of first page)

Application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions

In general, the Application System window (Program display window) configuration is required in the proxy interface to complete the page popup effect. (Appdelegate has UIWindow *window instance variable by default)
You can do this using the following code snippet:

Self.window = [[UIWindow alloc] Initwithframe:[[uiscreen mainscreen] bounds]];//display window configuration
Loginviewcontroller *login = [[Loginviewcontroller alloc] initwithnibname:@ "Loginviewcontroller" bundle:nil];// Initializes the first page of the application that needs to be displayed (typically Viewcontroller)
Self.window.rootViewController = login;//Configuring the Rootviewcontroller instance of the Windows window
Determine the system version, select Page load mode
if ([[Uidevice currentdevice].systemversion Floatvalue] < 6.0)
{
[Self.window AddSubview:login.view];
}
Else
{
[Self.window Setrootviewcontroller:login];
}
[Self.window makekeyandvisible];
return YES;


Conclusion: The use of appdelegate there are many, with the development of the application, I will continue to complement the perfect.

add: After the app is created, you can see the MAIN.M class file in the supporting files group, which is the first entry for the entire application.  
The code snippet executed is: Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class])); The last parameter is an instance of the Appdelegate class in the application, and the execution of this program enters the application didfinishlaunchingwithoptions in the Appdelegate class: (Nsdictionary * ) Launchoptions method interface.

UIApplication, Appdelegate, entrustment

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. &NBSP;&NBSP;&NBSP;&NBSP;UIAPPLICATIONSTATEACTIVE,&NBSP;&NBSP;
    3. &NBSP;&NBSP;&NBSP;&NBSP;UIAPPLICATIONSTATEINACTIVE,&NBSP;&NBSP;
    4. &NBSP;&NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;
    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

iOS Development appdelegate

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.