[Reprinted from the top of the article] iOS development UI-program startup principle and UIApplication, iosuiapplication

Source: Internet
Author: User

[Reprinted from the top of the article] iOS development UI-program startup principle and UIApplication, iosuiapplication
I,UIApplication1. Brief Introduction

(1) A UIApplication object represents an application. A UIApplication object represents an application.

(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.

(3) The singleton object can be obtained through [UIApplicationsharedApplication ].

(4) The first object created after an iOS program is started is the UIApplication object, and there is only one (get two UIApplication objects through code, and print the address to see that the address is the same ).

(5) use the UIApplication object to perform some application-level operations

2. Application-level operation example:

1) set the red alarm number in the upper-right corner of the application icon (for example, when a QQ message is sent, the icon displays 1, 2, and 3 new messages .)

@ Property (nonatomic) NSInteger applicationIconBadgeNumber;

Code implementation and effects:

-(Void) viewDidLoad {[super viewDidLoad]; // create and add a button UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (100,100, 60, 30)]; [btn setTitle: @ "button" forState: UIControlStateNormal]; [btn setBackgroundColor: [UIColor brownColor]; [btn addTarget: self action: @ selector (onClick) forControlEvents: Unknown]; [self. view addSubview: btn];}-(void) onClick {NSLog (@ "Click Event"); // error, only one unique UIApplication object exists, you can no longer create // UIApplication * app = [[UIApplication alloc] init]; // obtain the UIApplication object UIApplication * app = [UIApplication sharedApplication] of the program through sharedApplication; app. applicationIconBadgeNumber = 123 ;}2) set the visibility of the network indicator

@ Property (nonatomic, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;

Code and effects:

// Set the online animation app of the indicator. networkActivityIndicatorVisible = YES;

3) Manage the status bar

From iOS 7, the system provides two methods to manage the status bar.

A. Manage through UIViewController (each UIViewController can have its own different status bar ).

In iOS7, the status bar is managed by UIViewController by default. The following methods can be implemented by UIViewController to easily manage the visibility and style of the status bar.

Style of the status bar-(UIStatusBarStyle) preferredStatusBarStyle;

Status Bar visibility-(BOOL) prefersStatusBarHidden;

# Pragma mark-set the style of the status bar-(UIStatusBarStyle) preferredStatusBarStyle {// set it to white // return UIStatusBarStyleLightContent; // return UIStatusBarStyleDefault by default ;} # pragma mark-set whether the status bar is hidden (NO)-(BOOL) prefersStatusBarHidden {return NO ;}B. Manage through UIApplication (the status bar of an application is managed by it in a unified manner)

To use UIApplication to manage the status bar, you must first modify the settings of Info. plist.

Code:

// Obtain the UIApplication object UIApplication * app = [UIApplication sharedApplication]; app through sharedApplication. applicationIconBadgeNumber = 123; // sets the online animation app of the indicator. networkActivityIndicatorVisible = YES; // set the style of the status bar // app. statusBarStyle = UIStatusBarStyleDefault; // default (black) // set to white + animation effect [app setStatusBarStyle: UIStatusBarStyleLightContent animated: YES]; // set whether the status bar hides the app. statusBarHidden = YES; // set whether the status bar is hidden + animation effect [app setStatusBarHidden: YES withAnimation: UIStatusBarAnimationFade];C. Supplement

Since both of them can manage the status bar, when should we use it? If the style of the status bar is set only once, use UIApplication for management. If the status bar is hidden or the style is different, use the Controller for management. UIApplication for management has additional benefits and provides animation effects.

4) openURL: Method

UIApplication has a very powerful openURL: Method

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

OpenURL: some functions of the method include:

Call UIApplication * app = [UIApplicationsharedApplication]; [app openURL: [NSURLURLWithString: @ "tel: // 10086"];

Text message [app openURL: [NSURLURLWithString: @ "sms: // 10086"];

Mail [app openURL: [NSURLURLWithString: @ "mailto: // 12345@qq.com"];

Open a Web Resource [app openURL: [NSURLURLWithString: @ "http://ios.itcast.cn"];

Open the openURL method of other apps to open other apps.

URL supplement: URL: a uniform resource identifier used to uniquely represent a resource. URL format: protocol header: // host address/resource path network resource: http/ftp, etc. indicates the address of the image on Baidu (the host address is omitted) II,UIApplication Delegate

1. Simple Description

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.

There are many other similar situations that will cause app interference. When the app is disturbed, some system events will occur. At this time, UIApplication will notify its delegate object, let the delegate agent handle these system events.

Purpose: Notify the proxy to enter the background when it is interrupted. Each time a project is created, there is a class with the word "AppDelegate", which is the proxy of the UIApplication. By default, NJAppDelegate complies with the UIApplicationDelegate Protocol and is already the proxy of the UIApplication.

1 # import "YYAppDelegate. h "2 3 @ implementation YYAppDelegate 4 5 // when the application is started, it is called (automatically called by the System) 6-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions 7 {8 NSLog (@ "didfinishlaunchingwitexceptions"); 9 return YES; 10} 11 12 // calls when the activity state is about to lose (Focus loss, non-interactive) 13-(void) applicationWillResignActive :( UIApplication *) application14 {15 NSLog (@ "ResignActive"); 16} 17 18 // obtain the focus again (can interact with the user) 19-(void) applicationDidBecomeActive :( UIApplication *) application20 {21 NSLog (@ "BecomeActive "); 22} 23 24 // call 25 when the application enters the background. // generally, the data of the application is saved in this method, and the status is 26-(void) applicationDidEnterBackground :( UIApplication *) application27 {28 NSLog (@ "Background"); 29} 30 31 // when the application is about to enter the foreground, call 32 // This method to restore application data, and status 33-(void) applicationWillEnterForeground :( UIApplication *) application34 {35 NSLog (@ "Foreground "); 36} 37 38 // This method will be called when the application is about to be destroyed. 39 // Note: if the application is suspended, this method cannot be called. 40-(void) applicationWillTerminate :( UIApplication *) application41 {42} 43 44 // when the application receives a memory warning, it calls 45 // This method to release the unwanted memory 46-(void) applicationDidReceiveMemoryWarning :( UIApplication *) application47 {48 NSLog (@ "MemoryWarning"); 49} 50 @ endUIApplicationMain

The main function executes the UIApplicationMain function.

IntUIApplicationMain (int argc, char * argv [], NSString * principalClassName, NSString * delegateClassName );

Argc and argv: Pass them directly to UIApplicationMain for relevant processing.

PrincipalClassName: Specifies the application Class Name (symbol of the app), which must be a UIApplication (or subclass ). If it is nil, The UIApplication class is used as the default value.

DelegateClassName: Specifies the proxy class of the application, which must comply with the UIApplicationDelegate protocol.

The UIApplicationMain function creates a UIApplication object based on principalClassName, creates a delegate object based on delegateClassName, and assigns the delegate object to the delegate attribute in the UIApplication object.

Next, the Main Runloop (event loop) of the application will be created to process the event (the application: didfinishlaunchingwitexceptions: Method of the delegate object will be called after the program is completed)

The UIApplicationMain function is returned only when the program Exits normally.

# Import <UIKit/UIKit. h> # import "YYAppDelegate. h "int main (int argc, char * argv []) {@ autoreleasepool {// return UIApplicationMain (argc, argv, nil, NSStringFromClass ([YYAppDelegate class]); // return UIApplicationMain (argc, argv, @ "UIApplication", NSStringFromClass ([YYAppDelegate class]);/* argc: number of parameters input by the system or user argv: system or user-passed actual parameter 1. create a UIApplication object based on the third input parameter 2. create a proxy for the UIApplication object based on the fourth input. set the created proxy object to the proxy of UIApplication. 4. enable an event loop */return UIApplicationMain (argc, argv, @ "UIApplication", @ "YYAppDelegate ");}} 

System entry code and parameter description:

Argc: system or user-passed parameter argv: system or user-passed actual parameter 1. create a UIApplication object based on the third input parameter. create a proxy for the UIApplication object based on the fourth input. set the created proxy object to the proxy of UIApplication. 4. enable an event loop (it can be understood as an endless loop). This time loop is a queue (first-in-first-out) First added for processing. Ios program startup Principle4. Complete process of program startup

1. main Function

2. UIApplicationMain

* Create a UIApplication object

* Create the delegate object of the UIApplication.

 

3. The delegate object starts to process (Listen) system events (no storyboard)

* When the program is started, the agent's application: didfinishlaunchingwitexceptions: method will be called.

* Create a UIWindow in application: didfinishlaunchingwitexceptions:

* Create and set the rootViewController of the UIWindow.

* Display window

 

3. Obtain the file name of the primary storyboard Based on Info. plist and load the primary storyboard (with storyboard)

* Create a UIWindow

* Create and set the rootViewController of the UIWindow.

* Display window

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.