Program launches four objects (Uiapplication,appdelegate)
First, UIApplication
1. What is UIApplication?
UIApplication object is the symbol of the application • Each application has its own UIApplication object, and it is a singleton. • The singleton object can be obtained by [UIApplication Sharedapplication] · The first object created after an iOS program is started is the UIApplication object • Using UIApplication objects, you can perform someApplication LevelCommon Properties for Operation 2.UIApplication • Set the red reminder number in the upper right corner of the application icon
@property (nonatomic) Nsinteger applicationiconbadgenumber;
• Set the visibility of the networking indicator
@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;
The status bar in 3.ios7
• Starting from IOS7, the system provides 2 ways to manage the status bar byUiviewcontroller Management(Each uiviewcontroller can own a different status bar) ø throughuiapplication Management(The status bar of an application is managed uniformly by it)
• 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 disadvantage is that only the style and visibility of the status bar can be managed Others can only be managed with uiapplication)
Ø Style of the status bar
-(Uistatusbarstyle) Preferredstatusbarstyle;
Ø Visibility of the status bar
-(BOOL) Prefersstatusbarhidden;
•If you want to use uiapplication to manage the status bar, you must first modify the settings of the Info.plist
The following methods and properties can then be invoked with the object created by [UIApplication Sharedapplication] to control The style, visibility, and animation of the status bar
@property (nonatomic) Uistatusbarstyle Statusbarstyle; Default is Uistatusbarstyledefault
-(void) Setstatusbarstyle: (Uistatusbarstyle) Statusbarstyle animated: (BOOL) animated;
@property (Nonatomic,getter=isstatusbarhidden) BOOL Statusbarhidden;
-(void) Setstatusbarhidden: (BOOL) Hidden withanimation: (uistatusbaranimation) Animation
You can also control the frame and rotation direction of the status bar
@property (nonatomic) uiinterfaceorientation statusbarorientation;
-(void) Setstatusbarorientation: (uiinterfaceorientation) interfaceorientation animated: (BOOL) animated;
@property (nonatomic,readonly) Nstimeinterval statusbarorientationanimationduration;
@property (nonatomic,readonly) CGRect statusbarframe; Returns Cgrectzero if the status bar is hidden
4.openURL
UIApplication has a very powerful OpenURL: method
-(BOOL) OpenURL: (nsurl*) URL;
OpenURL: Part of the method function has o call
UIApplication *app = [UIApplication sharedapplication];
[App Openurl:[nsurl urlwithstring:@ "tel://10086"];
Ø Send SMS
[App Openurl:[nsurl urlwithstring:@ "sms://10086"];
Ø Send mail
[App Openurl:[nsurl urlwithstring:@ "Mailto://[email protected]"];
Ø Open a Web resource
[App Openurl:[nsurl urlwithstring:@ "http://ios.itcast.cn"];
Ø Open Other app programs
5.UIApplication and delegate
• 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 enter the background or even be terminated. There are many other similar situations that can cause the app to be disturbed, and when the app is disturbed, it generates some system events, and UIApplication notifies its delegate object. Let the delegate agent handle these system events delegate events that can be handled include: Ø Application lifecycle events (such as program startup and shutdown) Ø system events (such as incoming calls) O Memory warning o ...
Second, Appdelegate object1. • Each time a new project is completed, there is a class with the word "appdelegate", which is the agent of UIApplication
Appdelegate has complied with the Uiapplicationdelegate protocol by default and is already a proxy for uiapplication
2. Appdelegate object in the face of the proxy method of the call (there are many, such as the push notification of listening)//app after the start of the call
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
NSLog (@ "--didfinishlaunchingwithoptions");
return YES;
}
App loses focus (
One, hang up
When there is a phone coming in or lock screen, then your application will hang, at this time, Uiapplicationdelegate delegate will be notified, call the Applicationwillresignactive method, you can override this method, do the work before the hang, For example, close the network and save the data.
)
-(void) Applicationwillresignactive: (uiapplication *) Application {
NSLog (@ "--applicationwillresignactive");
}
When the app enters the background, the app's data (game data, such as pausing the game) is usually stored here.
-(void) Applicationdidenterbackground: (uiapplication *) Application {
NSLog (@ "--applicationdidenterbackground");
}
App Access Front desk
-(void) Applicationwillenterforeground: (uiapplication *) Application {
NSLog (@ "--applicationwillenterforeground");
}
The app gets the focus and it automatically calls this method (all controls can receive touch events as long as the program gets the focus)
Ii. Rehabilitation
When the program is restored, another named Applicationdidbecomeactive delegate method is called, where you can recover your application by saving data before suspending it:
Note: When the application starts, the Applicationdidbecomeactive method is called after the Applicationdidfinishlaunching method is called, so make sure that your code can distinguish between recovery and startup, Avoid logically occurring bugs.
-(void) Applicationdidbecomeactive: (uiapplication *) Application {
NSLog (@ "--applicationdidbecomeactive");
}
Clear memory that you do not need to use again
-(void) applicationdidreceivememorywarning: (uiapplication *) application
{
NSLog (@ "applicationdidreceivememorywarning");
}
Third, termination
When the user moves up or shuts down, the program will be terminated. This method performs the rest of the cleanup work, such as all connections that normally shut down and perform any other necessary work before the program exits.
-(void) Applicationwillterminate: (uiapplication *) Application {
NSLog (@ "--applicationdidbecomeactive");
}
Third, UIWindow
1. UIWindow is a special kind of uiview, usually in one app only one UIWindow iOS program is started, the first view control created is UIWindow, then the controller's view is created, Finally, the controller's view is added to the UIWindow, so the controller's view is displayed on the screen. • An iOS program can be displayed on the screen because it has uiwindow and says that without UIWindow, no UI interface is visible 2. There are two common ways of adding UIView to UIWindow: ø-(void) Addsubview: (UIView *) view;
The view is added directly to the UIWindow, but the view corresponding Uiviewcontroller is not ignored
Ø@property (Nonatomic,retain) Uiviewcontroller *rootviewcontroller;
Automatically add Rootviewcontroller view to UIWindow, responsible for managing the Rootviewcontroller lifecycle
• Common methods
ø-(void) Makekeywindow;
Make current UIWindow into Keywindow (main window)
øø-(void) makekeyandvisible;
Make the current UIWindow into Keywindow and show it
3.UIWindow acquisition of [uiapplication Sharedapplication].windows
A list of UIWindow that opens in this app so you can touch any of the UIView objects in your app
(usually input text pop-up keyboard, in a new UIWindow)
[uiapplication Sharedapplication].keywindow
The UIWindow used to receive message events for keyboards and non-touch classes, and only one uiwindow per time in a program is Keywindow. If a text box inside a UIWindow cannot enter text, it may be because this uiwindow is not Keywindow
View.window
Get the UIWindow where a uiview is located
At last
The 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
Program Start principle (four objects)