1.0 UIApplication object, 1.0 uiapplication

Source: Internet
Author: User

1.0 UIApplication object, 1.0 uiapplication

Features of the UIApplication object: Feature 1:
  • A UIApplication object represents an application and is a singleton object. (It is used to encapsulate an object of the entire application, such as what to do when the application is executed to a certain period, and the lifecycle .)
  • Obtain the UIApplication object: [UIApplication sharedApplication]
  • After an iOS program is started, the first object created is the UIApplication object, and there is only one.
For example, you can obtain two UIApplication objects by code. The printed Address shows that the addresses are the same.
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 4 // obtain the app object to prove that one app only has one UIApplication object 5 UIApplication * app1 = [UIApplication sharedApplication]; 6 7 UIApplication * app2 = [UIApplication sharedApplication]; 8 NSLog (@ "app1 = % p ---- app2 = % p", app1, app2); 9 10}
Print result:13:34:58. 847 01 UIApplication [4563: 852645] app1 = 0x7fe2b3e0eaa0 ---- app2 = 0x7fe2b3e0eaa0
Feature 2:
  • Each application has its own UIApplication object, which is a singleton.
  • If you try to create a new UIApplication object in the program, an error is reported.
Example:
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 4 // create a UIApplication object through alloc + init, 5 UIApplication * app = [[UIApplication alloc] init]; 6 7 NSLog (@ "% p", app); 8}
Print result:13:39:00. 270 01 UIApplication [4675: 867735] *** Terminating app due to uncaught exception 'nsinternalinconsistencyexception', reason: 'There can only be one UIApplication instance .'
Feature 3:
  • Use the UIApplication object to perform some application-level operations
(1) apply the icon to display numbers (For details, refer to 1.1 common attributes)
(2) Wait icon indicator on the status bar (For details, refer to 1.1 common attributes)
(3) Use UIApplication to open a resource (openURL: method). The ghost system automatically identifies and uses an app to open the resource based on the protocol.
1 // open a web page: 2 [app openURL: [NSURL URLWithString: @ "http://ios.icast.cn"]; 3 4 // call 5 [app openURL: [NSURL URLWithString: @ "tel: // 10086"]; 6 7 // send a text message 8 [app openURL: [NSURL URLWithString: @ "sms: // 10086"]; 9 10 // mail 11 [app openURL: [NSURL URLWithString: @ "mailto: // 12345@qq.com"]; 12
Using the openURL method, ingress can also open other applications and call each other between different applications. Meitu xiuxiu, click share to "Sina Weibo", open "Sina Weibo", select an account, jump back to "meitu xiuxiu", start sharing the Himalayan, and log on with Weibo and QQ accounts. You must jump between applications.
(4) Manage the status bar through UIApplication (For details, refer to "1.2)
1.1-common attributes Common attributes of UIApplication
1 // red reminder number (0 by default) in the upper right corner of the application icon 2 @ property (nonatomic) NSInteger applicationIconBadgeNumber; 3 4 EXAMPLE: 5 6-(void) viewDidLoad {7 [super viewDidLoad]; 8 9 // obtain the singleton object 10 UIApplication * app = [UIApplication sharedApplication]; 11 12 // create user notification settings (requires user permission to set notifications after iOS8) 13 if ([[[UIDevice currentDevice] systemVersion] floatValue]> = 8.0) {14 UIUserNotificationSettings * settings = 15 [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeBadge16 categories: nil]; 17 // register permission 18 [app registerUserNotificationSettings: settings]; 19} 20 21 // set the number 22 app. applicationIconBadgeNumber = 10; 23 24}
1 // visibility of the network indicator (no by default) 2 @ property (nonatomic, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
3 Example: 4 5-(void) viewDidLoad {6 [super viewDidLoad]; 7 8 // obtain the singleton object 9 UIApplication * app = [UIApplication sharedApplication]; 10 11 // set network Indicator 12 app. networkActivityIndicatorVisible = YES; 13 14}

 

  1.2-manage the status bar The system provides two methods to manage the status bar. 1. Manage through UIViewController (after iOS 7) (each UIViewController can have its own different status bar) (recommended)
1 // style of the status bar 2-(UIStatusBarStyle) preferredStatusBarStyle; 3 4 // visibility of the status bar 5-(BOOL) prefersstatusbarhid; 6 // status bar visibility (no by default) --- UIViewController Management (recommended) 7-(BOOL) prefersStatusBarHidden {8 return NO; 9} 10 11 // style of the status bar --- UIViewController Management (recommended) 12-(UIStatusBarStyle) preferredStatusBarStyle {13 14/* status bar style statusBarStyle15 UIStatusBarStyleDefault status bar is black 16 UIStatusBarStyleLightContent status bar is white 17 blank */20 21 return UIStatusBarStyleDefault; 22}

 

2. Manage through UIApplication (before iOS 6)
(1) If you want to use UIApplication management after ios 7, add a configuration item in the Info. plist file.
  • Key: View controller-based status bar appearance
  • Value: NO
1 // style of the status bar --- UIApplication Management 2-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {3 4 // obtain the singleton object 5 UIApplication * app = [UIApplication sharedApplication]; 6 7 // set the style of the status bar with animation effects-from black to white (expired) 8 [app setStatusBarStyle: UIStatusBarStyleLightContent animated: YES]; 9 10 // hide the status bar 11 app. statusBarHidden = YES; 12 13 // hide the status bar in an animated way (expired ios 3.2 is discarded) 14 [app setStatusBarHidden: YES animated: YES]; 15 16/* withAnimation: method (expired) 17 UIStatusBarAnimationNone no special effect 18 fade out effect 19 UIStatusBarAnimationSlide up exit hide 20 */21 [app setStatusBarHidden: YES withAnimation: UIStatusBarAnimationSlide]; 22}
Usage  
UIApplication: Set the style of the status bar only once. Use the animation effect.
UIViewController: whether the status bar is hidden. The style is different.
1.3-UIApplicationDelegate Introduction  
  • All mobile operating systems have a fatal drawback: apps are easily disturbed.
  • For example, an incoming call or screen lock may cause the app to enter the background or even be terminated, and many other similar situations may cause app interference.
  • When an app is disturbed, some system events are generated. In this case, UIApplication notifies its delegate object to process these system events.
Events that can be handled by delegate include:
  • Application lifecycle events (such as program startup and shutdown)
  • System events (such as laidian)
  • Memory warning
  • ... ...
Introduction:  
  • The AppDelegate file after the project is created is the proxy object of the UIApplication.
  • The main function of the proxy object has been set. You do not need to set it manually.
Settings in the main function:
File Location: Supporting Files-> main. m
1 # import <UIKit/UIKit. h> 2 # import "AppDelegate. h "3 4 int main (int argc, char * argv []) {5 @ autoreleasepool {6 7 // set the UIApplication object to be started, and the corresponding proxy object AppDelegate 8 return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]); 9} 10}
Note:  
The main function of AppDelegate is to process (Listen) various events of the application itself.
To become a proxy object of the UIApplication, you must comply with the: UIApplicationDelegate protocol.
1.3.1-proxy method By default, the AppDelegate file complies with the UIApplicationDelegate Protocol and is already the proxy of UIApplicationDelegate.
1 //  AppDelegate.h2  3 #import <UIKit/UIKit.h>4 5 @interface AppDelegate : UIResponder <UIApplicationDelegate>6 7 @property (strong, nonatomic) UIWindow *window;8  9 @end
Process (Listen) various events of the application itself:
1 // AppDelegate. m 2 3 # import "AppDelegate. h "4 5 @ interface AppDelegate () 6 7 @ end 8 9 @ implementation AppDelegate10 11 // After the app is started, this method will not be called again! 12 // if the application is killed by the operating system due to memory or other reasons, click the icon again to call this method! 13-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {14 NSLog (@ "% s", _ func _); 15 return YES; 16} 17 18 // called when the app is about to exit the activity state (will lose focus: cannot interact with the user) (this method is called when a call or SMS is sent) 19 // the game should be suspended in this method! This method is especially important in game development! 20-(void) applicationWillResignActive :( UIApplication *) application {21 NSLog (@ "% s", _ func __); 22} 23 24 // The app has entered the background to call this method (Store application data and status in this method) 25 // The app exits to the background and releases shared resources, save User Data, stop the clock, and save enough application status information... 26-(void) applicationDidEnterBackground :( UIApplication *) application {27 NSLog (@ "% s", _ func __); 28} 29 30 // this method is called when the app is about to enter the foreground (the data and status of the application are restored in this method) 31-(void) applicationWillEnterForeground :( UIApplication *) application {32 NSLog (@ "% s", _ func _); 33} 34 35 // The app is active, restart the previously paused status (re-obtain the focus: You can interact with the user) 36-(void) applicationDidBecomeActive :( UIApplication *) application {37 38 NSLog (@ "% s ", _ func _); 39} 40 41 // The app receives a memory warning and calls this Method 42-(void) applicationDidReceiveMemoryWarning :( UIApplication *) application {43 NSLog (@ "% s", _ func _); 44} 45 46 // The app is about to be killed call this method 47 // Note: 1. active shutdown 2. the program 48-(void) applicationWillTerminate :( UIApplication *) application {49 50 NSLog (@ "% s", _ func _) is disabled because of insufficient memory __); 51} 52 @ end

 

 
If you have any questions, please send an e-mail to shorfng@126.com to contact me. By: Loto)

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.