UIApplication and proxy for iOS learning, iosuiapplication

Source: Internet
Author: User

UIApplication and proxy for iOS learning, iosuiapplication
1. What is UIApplication?

  • A UIApplication object is a symbolic object of an application. It cannot be created manually or alloc init. Only one application can be created.

  • Each application has its own UIApplication object and is a singleton.

You can use [UIApplication processing application] to obtain the singleton object.

Reasons for creating a singleton:

The UIApplication object is used to set the global information of the application. If an application has many UIApplication objects, it does not know who to listen.

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

  • Use the UIApplication object to perform some application-level operations.

2. Functions of UIApplication (common attributes and methods)

UIApplication is generally used for some application-level operations (the app reminder box controls the networking status, calls, and opens a webpage ).

1> set the appIcon reminder number

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

Attribute declaration 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 registerUserNotificationSettings:] before being able to set the icon badge.

Note that after iOS8, you must register a user notification before the notification number is displayed.

Registration user notification method statement:

// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

The purpose of iOS8.0 to launch a user registration notification is to improve the user experience. Users with obsessive-compulsive disorder can use this notification to disable the reminder number.

Instance code:

// Obtain the UIApplication object UIApplication * app = [UIApplication sharedApplication] through the singleton method; // set the appIcon reminder number. After iOS8, you must register the user notification app. applicationIconBadgeNumber = 10; // create user notification uiusernotificationicationsettings * settings = [UIUserNotificationSettings failed: uiusernotiftypetypebadge categories: nil]; // notification for registering a user [app registerUserNotificationSettings: settings];

User notification

Click "OK" to see a reminder similar to the number of QQ messages,

Click "not allowed,

2> set the networking status

The network status is displayed, indicating that the application is online.

Attribute declaration in the iOS header file:

@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible __TVOS_PROHIBITED; // showing network spinning gear in status bar. default is NO

Instance code:

// Set the networking status app. networkActivityIndicatorVisible = YES;

:

3> open the webpage

Method declaration in the iOS header file:

- (BOOL)openURL:(NSURL*)url NS_EXTENSION_UNAVAILABLE_IOS("");

Instance code:

# Pragma mark-open webpage-(IBAction) btnClick :( id) sender {// URL: Resource path // URL: protocol header: // domain name + path http, https, file, tel // protocol header: // open the web page @ "http://www.baidu.com" NSURL * url = [NSURL URLWithString: @ "http://www.baidu.com"]; [[UIApplication sharedApplication] openURL: url];}

The advantage of using UIApplication to open resources: the system automatically determines which software is used to open resources based on the protocol header.

3. Proxy method of UIApplication

We can go to the header file of UIApplicationDelegate to see that there are many proxy methods, which are commonly generated in AppDelegate when you create a Single View Application project. The following code is, for details about how to use these proxies, see the code annotations.

Instance code:

# Import "AppDelegate. h "@ interface AppDelegate () @ end @ implementation AppDelegate // learn the proxy method. You only need to know when to call this method, what can this method do? // call (commonly used)-(BOOL) application after the program is started: (UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. NSLog (@ "% s __% d", _ FUNCTION __, _ LINE _); return YES;} // call-(void) when the app loses focus) applicationWillResignActive :( UIApplication *) application {NSLog (@ "% s __% d", _ FUNCTION __, _ LINE __); // Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} // call the app when it enters the background (frequently used) // when the app is suddenly interrupted, save the required data-(void) applicationDidEnterBackground :( UIApplication *) application {NSLog (@ "% s __% d", _ FUNCTION __, _ LINE _); // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits .} // call-(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 changes of the changes made on entering the background .} // called when the app gets the focus, which means the app can interact with the user-(void) applicationDidBecomeActive :( UIApplication *) application {NSLog (@ "% s __% d ", _ FUNCTION __, _ LINE _); // Restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} // call-(void) applicationWillTerminate :( UIApplication *) application {NSLog (@ "% s __% d", _ FUNCTION __, _ LINE _); // Called when the application is about to terminate. save data if appropriate. see also applicationDidEnterBackground :.} // when the app receives a memory warning, it is called (frequently used) // clear the image cache-(void) applicationDidReceiveMemoryWarning :( UIApplication *) application {NSLog (@ "% s __% d ", _ FUNCTION __, _ LINE _);} @ end

 

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.