IOS development UI Article 16 program startup principle and UIApplication
IOSDevelopmentUIArticle-Program startup principles andUIApplication
I,UIApplication
1. 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:
-(IBAction) ChangeAppNum {
// Error. Only one unique UIApplication object can be created.
// UIApplication * app = [[UIApplication alloc] init];
UIApplication* App = [UIApplicationSharedApplication];
// 0 indicates clearing the number in the upper-right corner of the icon
App.ApplicationIconBadgeNumber=20;
}
2) set the visibility of the network indicator
@ Property (nonatomic, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
Code and effects:
// Sets the online animation of the indicator.
App.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;
// Black by default
ReturnUIStatusBarStyleDefault;
}
# Pragma mark-set whether the status bar is hidden (NO)
-(BOOL) PrefersStatusBarHidden
{
ReturnNO;
}
}
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:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + IDxzdHJvbmc + primary + PTxzdHJvbmc + primary + OzwvcD4KPHA + primary + Ly/primary + ICAgCjxzdHJvbmc + Ly/primary + signature + signature/HS/Signature + signature + OjxzdHJvbmc + signature + XTs8L3A + signature + vMjIu8G91ta2vL/Signature + signature/2NbGxve9 + signature + 6GjPC9wPgo8cD4KIDSjqW9wZW5VUkw6t723qDwvcD4KPHA + signature + Signature = "tel: // 10086 "];
Text message [app openURL: [NSURLURLWithString: @ "sms: // 10086"];
Mail [appopenURL: [NSURLURLWithString: @ "mailto: // 12345@qq.com"];
Open a Web Resource [appopenURL: [NSURLURLWithString: @ "http://ios.itcast.cn"];
Open the openURL method of other apps to open other apps.
URL supplement:
URL: a unified resource identifier that uniquely identifies a resource.
URL format: protocol header: // host address/resource path
Network Resources: http/ftp, said Baidu on a picture of the address http://www.baidu.com/images/20140603/abc.png
Local Resource: file: // users/apple/desktop/abc.png (host address 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.
2. Proxy Method
/**
* After the app is started, it will call
*/
-(BOOL) Application :(UIApplication*) Application didfinishlaunchingwitexceptions :(NSDictionary*) LaunchOptions
{
NSLog(@ "% @",Self.Window);
NSLog(@ "Didfinishlaunchingwitexceptions");
// Override point for customizationafter application launch.
ReturnYES;
}
-(Void) ApplicationWillResignActive :(UIApplication*) Application
{
}
/**
* Called when the app enters the background
*
* Generally, the APP data is stored here (game data, such as suspending the game)
*/
-(Void) ApplicationDidEnterBackground :(UIApplication*) Application
{
NSLog(@ "ApplicationDidEnterBackground");
}
-(Void) ApplicationWillEnterForeground :(UIApplication*) Application
{
NSLog(@ "ApplicationWillEnterForeground");
.
}
-(Void) ApplicationDidBecomeActive :(UIApplication*) Application
{
}
/**
* Clear unnecessary memory.
*/
-(Void) ApplicationDidReceiveMemoryWarning :(UIApplication*) Application
{
NSLog(@ "ApplicationDidReceiveMemoryWarning");
}
-(Void) ApplicationWillTerminate :(UIApplication*) Application
{
}
@ End
Applications generally have five statuses: Official Document app. states
Iii. Program startup Principle
UIApplicationMain
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
# Import"TXAppDelegate. h"
IntMain (IntArgc,Char* Argv [])
{
@ Autoreleasepool{
// Return UIApplicationMain (argc, argv, nil, NSStringFromClass ([TXAppDelegate class]);
@ Autoreleasepool{
// ReturnUIApplicationMain (argc, argv, nil, @ "MJAppDelegate ");
// ReturnUIApplicationMain (argc, argv, @ "UIApplication", @ "MJAppDelegate ");
// 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: the actual parameters passed by the system or user.
1. Create a UIApplication object based on the third input parameter
2. Create a proxy for the UIApplication object based on the fourth passed-in
3. Set the created proxy object to the proxy of the UIApplication.
4. Enable an event Loop
*/
ReturnUIApplicationMain(Argc, argv,NSStringFromClass([UIApplicationClass]),NSStringFromClass([TXAppDelegateClass]);
}
}
System entry code and parameter description:
Argc: system or user-passed parameters
Argv: the actual parameter passed by the system or user.
1. Create a UIApplication object based on the third input parameter
2. Create a proxy for the UIApplication object based on the fourth passed-in
3. Set the created proxy object to the proxy of the 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.
IosProgram startup Principle
4. 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