Original http://blog.csdn.net/u010927311/article/details/40145035
IOS Development UI Chapter - program start principle and uiapplication
First, uiapplication
1. Brief introduction
(1) The UIApplication object is a symbol of the application, and a UIApplication object represents an application.
(2) Each application has its own UIApplication object, and is a singleton, if you try to create a new UIApplication object in the program, then the error prompt.
(3) This singleton object can be obtained by [Uiapplicationsharedapplication]
(4) An IOS program is created after the first object is the UIApplication object, and only one (through the code to get two UIApplication objects, the print address can see the address is the same).
(5) Use the UIApplication object to perform some application-level operations
2. Application-level operation examples:
1) Set the application icon in the upper right corner of the red reminder number (such as QQ message, the icon will show 1, 2, 3 new information, etc.). )
@property (nonatomic) Nsinteger applicationiconbadgenumber;
Code implementation and Effects:
-( ibaction ) changeappnum {
Error, there can be only one unique UIApplication object that can no longer be created
Uiapplication*app=[[uiapplication Alloc]init];
uiapplication *app = [ uiapplication sharedapplication ];
0 to clear the upper right corner of the icon
App. applicationiconbadgenumber = ;
}
2) Set the visibility of the networking indicator
@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;
Code and Effects:
Set up an indicator's networked animation
App. networkactivityindicatorvisible = YES ;
3) Manage status bar
Starting with IOS7, the system provides 2 ways to manage the status bar
A. Through Uiviewcontroller management (each uiviewcontroller can have its own different status bar).
In IOS7, by default, the status bar is managed by Uiviewcontroller, and Uiviewcontroller can easily manage the visibility and style of the status bar by implementing the following methods
The style of the status bar-(Uistatusbarstyle) Preferredstatusbarstyle;
Visibility of the status bar-(BOOL) Prefersstatusbarhidden;
#pragma mark-to set the style of the status bar
-(Uistatusbarstyle) Preferredstatusbarstyle
{
Set to White
return uistatusbarstylelightcontent;
Default is Black
return Uistatusbarstyledefault;
}
#pragma mark-set whether the status bar is hidden (no)
-( BOOL ) Prefersstatusbarhidden
{
return NO ;
}
}
B. Through UIApplication management (the status bar of an application is managed by it uniformly)
If you want to use uiapplication to manage the status bar, you first need to modify the Info.plist settings
Code:
uiapplication *app=[uiapplication sharedapplication];
App. applicationiconbadgenumber = 123 ;
Set up an indicator's networked animation
App. networkactivityindicatorvisible = YES ;
Set the style of the status bar
app.statusbarstyle=uistatusbarstyledefault;//Default (Black)
Set to white + animated effect
[App Setstatusbarstyle : uistatusbarstylelightcontent animated : YES ];
Set whether the status bar is hidden
App. Statusbarhidden = YES ;
Sets whether the status bar hides + animated effects
[App Setstatusbarhidden : YES withanimation : uistatusbaranimationfade ];
C. Supplementary
Since both can be managed on the status bar, when should you use it?
If the style of the status bar is set only once, use uiapplication to manage it.
If the status bar is hidden and the style is different, then use the controller to manage it.
UIApplication to manage has additional benefits that can provide animation effects.
4) OpenURL: Method
UIApplication has a very powerful OpenURL: method
-(BOOL) OpenURL: (nsurl*) URL;
OpenURL: Some of the functions of the method are
Call UIApplication *app =[uiapplicationsharedapplication]; [appopenurl:[nsurlurlwithstring:@ "tel://10086"];
Send SMS [app openurl:[nsurlurlwithstring:@ "sms://10086"];
e-Mail [appopenurl:[nsurlurlwithstring:@ "Mailto://[email protected]"];
Open a webpage resource [appopenurl:[nsurlurlwithstring:@ "http://ios.itcast.cn"];
Open another app OpenURL method to open other apps.
URL Additions:
URL: A Uniform Resource locator that is used to uniquely represent a resource.
URL format: protocol header://Host address/resource path
Network resources: HTTP/FTP, such as Baidu on a picture of the address http://www.baidu.com/images/20140603/abc.png
Local resources: File:///users/apple/desktop/abc.png (host address omitted)
Second, uiapplication Delegate
1. Brief description
All mobile operating systems have a fatal disadvantage: apps are vulnerable to interruptions. For example, a call or lock screen will cause the app to go backstage or even be terminated.
There are a number of other similar situations that can cause the app to be disturbed, causing some system events when the app is disturbed, and UIApplication notifies its delegate object, allowing delegate agents to handle these system events.
Function: When interrupted, notifies the agent to enter the background.
After each new project, there is a class with the word "appdelegate", it is the uiapplication agent, njappdelegate Default has complied with the Uiapplicationdelegate protocol, has been UIApplication's agent.
2. Proxy methods
/**
* When the app is finished, it will be called
*/
-( BOOL ) Application: ( uiapplication *) application didfinishlaunchingwithoptions: ( nsdictionary *) launchoptions
{
NSLog ( @ "%@" , self. window );
NSLog ( @ "didfinishlaunchingwithoptions" );
Override point for Customizationafter application launch.
return YES ;
}
-( void ) Applicationwillresignactive: ( uiapplication *) application
{
}
/**
* Called when the app enters the background
*
* Generally save the app's data (game data, such as pause game)
*/
-( void ) Applicationdidenterbackground: ( uiapplication *) application
{
NSLog ( @ "Applicationdidenterbackground" );
}
-( void ) Applicationwillenterforeground: ( uiapplication *) application
{
NSLog ( @ "Applicationwillenterforeground" );
.
}
-( void ) Applicationdidbecomeactive: ( uiapplication *) application
{
}
/**
* Clear memory that you do not need to use again
*/
-( void ) Applicationdidreceivememorywarning: ( uiapplication *) application
{
NSLog ( @ "applicationdidreceivememorywarning" );
}
-( void ) Applicationwillterminate: ( uiapplication *) application
{
}
@end
Applications typically have five statuses: Official document App.states
Three, the principle of program start
Uiapplicationmain
A uiapplicationmain function is executed in the main function.
Intuiapplicationmain (int argc, char *argv[], nsstring *principalclassname, NSString *delegateclassname);
ARGC, argv: direct transfer to Uiapplicationmain for related processing can be
Principalclassname: Specifies the application class name (symbol of the app), which must be uiapplication (or subclass). If nil, use the UIApplication class as the default value
Delegateclassname: Specifies the application's proxy class, which must comply with the Uiapplicationdelegate protocol
The Uiapplicationmain function creates a UIApplication object based on Principalclassname, creates a delegate object from Delegateclassname, and assigns the delegate object to the UI The delegate property in the Application object
The application's Main Runloop (the event loop) is then created, and the event is processed (the application:didfinishlaunchingwithoptions of the delegate object is called first after the program is completed: method)
The Uiapplicationmain function returns when the program exits normally
#import <UIKit/UIKit.h>
#import "TXAppDelegate.h"
int Main ( int argc, 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 passed in by the system or user
ARGV: Actual parameters passed in by the system or user
1. Create a UIApplication object based on the third parameter passed in
2. Based on the fourth incoming agent that created the UIApplication object
3. Set the proxy object that you just created for uiapplication
4. Turn on an event loop
*/
return Uiapplicationmain (argc, argv, nsstringfromclass ([ uiapplication class ]), nsstringfromclass ([ Txappdelegate class ]));
}
}
Code and parameter description for system entry:
ARGC: System or user-passed parameters
ARGV: Actual parameters passed in by the system or user
1. Create a UIApplication object based on the third parameter passed in
2. Based on the fourth incoming agent that created the UIApplication object
3. Set the proxy object that you just created for uiapplication
4. Turn on an event loop (which can be understood as a dead loop inside) This time loop is a queue (FIFO) first added first processing
iOS Program Start principle
Iv. complete process of program initiation
1.main function
2.UIApplicationMain
* Create UIApplication objects
* Create a UIApplication delegate object
3.delegate object starts processing (listening) system events (no storyboard)
* When the program is started, the agent's application:didfinishlaunchingwithoptions is called: method
* Create UIWindow in application:didfinishlaunchingwithoptions:
* Create and set UIWindow Rootviewcontroller
* Display window
3. According to Info.plist to obtain the most important storyboard file name, load the main storyboard (with storyboard)
* Create UIWindow
* Create and set UIWindow Rootviewcontroller
* Display window
iOS Development UI Chapter 16 program start principle and UIApplication