iOS learning UIApplication and its agents

Source: Internet
Author: User

1. What is UIApplication
    • The uiapplication object is symbolic of the application, cannot be created manually, cannot alloc init, and an application allows only one .

    • Each app has its own uiapplication object, and it's a singleton .

  This singleton object can be obtained by [uiapplication shareapplication].  

Reasons for a single case:

   uiapplication object is used to set the application of global information, an application if there are many uiapplication objects, do not know who to listen to.

    • The first object that is created after an iOS program is started is the uiapplication object.

    • With uiapplication objects, you can perform some application-level operations.

2. The role of uiapplication (Common properties and methods)

uiapplication is generally used to do some application-level actions (App Reminder box, control the status of the network, call, open Web page).

1> Set AppIcon reminder number

The icon needs to be cleared manually , the application is closed and not automatically cleared.

Attribute declarations in the iOS header file:

@property (nonatomic) Nsinteger applicationiconbadgenumber __tvos_prohibited;  // set to 0 to hide. Default is 0. In IOS 8.0 and later, your application must register for user notifications using-[uiapplication registerusernotification Settings:] Before being able to set the icon badge.

Through comments we can be informedthat: IOS8 must be registered after the user notification, registration to display the reminder number

  Registered user Notification method declaration:

// registering uiusernotificationsettings more than once results in previous settings being overwritten. -(void) Registerusernotificationsettings: (uiusernotificationsettings *) notificationsettings NS_AVAILABLE _ios (8_0) __tvos_prohibited;

iOS8.0 The purpose of introducing registered user notifications is to improve the user experience , and for users with obsessive-compulsive disorder, the reminder will be turned off by this notification.

Instance code:

// get the UIApplication object by using a singleton method UIApplication *app = [uiapplication sharedapplication]; // Set AppIcon reminder number, iOS8 must register user notification after Ten ; // Create a user notification Uiusernotificationsettings *settings = [uiusernotificationsettings settingsfortypes: Uiusernotificationtypebadge Categories:nil]; // Notification of registered users [App Registerusernotificationsettings:settings];

User notification

Click " OK " After you can see similar to the number of QQ messages reminders,

No after clicking " not allowed ",

2> setting Up Networking status

  Displays the networking status, telling the user that the app is networked.  

Attribute declarations in the iOS header file:

// showing network spinning gear in status bar. Default is NO

Instance code:

    // set up Networking status    app.networkactivityindicatorvisible = YES;

3> Open Web page

  Method declarations in the iOS header file:

-(BOOL) OpenURL: (nsurl*) URL Ns_extension_unavailable_ios ("");

Instance code:

#pragmaMark-Open Web page-(ibaction) Btnclick: (ID) Sender {//URL: Resource Path//URL: protocol header://domain + path Http,https,file,tel//protocol Header://Open Web page @ "http://www.baidu.com"Nsurl*url = [Nsurl urlwithstring:@"http://www.baidu.com"]; [[UIApplication sharedapplication] openurl:url];}

uiapplication The benefits of opening a resource: Without judging what software to open, the system will automatically be judged by the protocol header.

3. Proxy method for UIApplication

We can go into Uiapplicationdelegate's header file to see that it has a lot of proxy methods, commonly known as you create a single View application project in Appdelegate generated, the following code is, The details of how these agents are used are described in code comments.

Instance code:

#import "AppDelegate.h"@interfaceappdelegate ()@end@implementationappdelegate//Learn the proxy method, just need to know when this method is called, this method can be used to do//called after the program starts (common)-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Override point for customization after application launch.NSLog (@"%s__%d", __function__, __line__); returnYES;}//called when the app loses focus- (void) Applicationwillresignactive: (UIApplication *) Application {NSLog (@"%s__%d", __function__, __line__); //Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US    Er quits the application and it begins the transition to the background state. //Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}//call when the app goes Backstage (common)//when the app is suddenly interrupted, save some of the data it needs to use here .- (void) Applicationdidenterbackground: (UIApplication *) Application {NSLog (@"%s__%d", __function__, __line__); //Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in    Formation to the restore your application to the it is terminated later. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits.}//called when the app is about to enter the foreground- (void) Applicationwillenterforeground: (UIApplication *) Application {NSLog (@"%s__%d", __function__, __line__); //Called as part of the transition from the background to the inactive state; Here you can undo many of the changes mad E on entering the background.}//called when the app gets to the focus, meaning the app can interact with the user- (void) Applicationdidbecomeactive: (UIApplication *) Application {NSLog (@"%s__%d", __function__, __line__); //Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.}//called when the app is closed- (void) Applicationwillterminate: (UIApplication *) Application {NSLog (@"%s__%d", __function__, __line__); //Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.}//The app is called when it accepts memory warnings (common)//Empty Picture Cache- (void) Applicationdidreceivememorywarning: (UIApplication *) application{NSLog (@"%s__%d", __function__, __line__);}@end

iOS learning UIApplication and its agents

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.