iOS Development UI Chapter-Program startup principle and UIApplication

Source: Internet
Author: User
Tags uikit

iOS Development UI Chapter-Program startup principle and UIApplication First,UIApplication1. 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 a new message, such as three-way bar. )

@property (nonatomic) Nsinteger applicationiconbadgenumber;

Code implementation and Effects:

-(void) viewdidload{    [Super Viewdidload];    Create and add a button    UIButton *btn=[[uibutton alloc]initwithframe:cgrectmake (+, +)];    [Btn settitle:@ "button" forstate:uicontrolstatenormal];    [Btn Setbackgroundcolor:[uicolor Browncolor];    [Btn addtarget:self Action: @selector (OnClick) forcontrolevents:uicontroleventtouchupinside];    [Self.view addsubview:btn];} -(void) onclick{    NSLog (@ "button click event");    Error, can only have a unique uiapplication object, can not be created//    uiapplication *app=[[uiapplication alloc]init];        Obtain the UIApplication object of the program by Sharedapplication    uiapplication *app=[uiapplication sharedapplication];    app.applicationiconbadgenumber=123;}

2) Set the visibility of the networking indicator

@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;

Code and Effects:

  Set the 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-Set the style of the status bar-(Uistatusbarstyle) preferredstatusbarstyle{    //set to white    //return uistatusbarstylelightcontent;    The 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:

  Obtain the UIApplication object of the program by Sharedapplication    uiapplication *app=[uiapplication sharedapplication];    app.applicationiconbadgenumber=123;        Set the indicator's networked animation    App.networkactivityindicatorvisible=yes;    Set the style of the status bar    //app.statusbarstyle=uistatusbarstyledefault;//default (Black)    //set to white + animated effects      [app Setstatusbarstyle:uistatusbarstylelightcontent Animated:yes];    Sets whether the status bar hides    App.statusbarhidden=yes;      Sets whether the status bar hides + Animates    [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, and if the status bar is hidden, the style is different then it is managed with the controller. 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]; [App openurl:[nsurlurlwithstring:@ "tel://10086"];

Send SMS [app openurl:[nsurlurlwithstring:@ "sms://10086"];

email [app openurl:[nsurlurlwithstring:@ "Mailto://[email protected]"];

Open a Web page resource [app openurl:[nsurlurlwithstring:@ "http://ios.itcast.cn"];

Open another app OpenURL method to open other apps.

URL supplement: 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 and so on to represent the address of Baidu on a picture 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.

Each new project, there is a "appdelegate" word of the class, it is the uiapplication agent, njappdelegate by default has complied with the Uiapplicationdelegate protocol, is already an agent of uiapplication.

2. Proxy methods
 1 #import "YYAppDelegate.h" 2 3 @implementation yyappdelegate 4 5//When the application is started, it will be called (System Auto-Call) 6-(BOOL) application: (UI Application *) Application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8 NSLog (@ "DidFinishLaunchin Gwithoptions "); 9 return yes;10}11 12//is about to be inactive (lose focus, not interactive) (void) Applicationwillresignactive: (uiapplication *) application NSLog (@ "resignactive"), 16}17 18//regain focus (ability to interact with user)-(void) Applicationdidbecomeactive: (uiapplication *) appli Cation20 {NSLog (@ "becomeactive"); 22}23 24//When the application enters the background, it calls 25//generally saves the application's data in the method, as well as the status of-(void) applicationdident Erbackground: (uiapplication *) application27 {NSLog (@ "Background"); 29}30 31//application is about to enter the foreground when the call 32//general in this method to recover the number of applications (void) Applicationwillenterforeground: (uiapplication *) application34 {NSLog (@ "Foreground"), 36}37 38// This method is called when the program is about to be destroyed 39//Note: The method cannot be called if the application is in a suspended state (void) Applicationwillterminate: (uiapplication *) application41 { 42}43 44//ApplicationWhen the program receives a memory warning, it calls 45//generally frees the unwanted memory in the method (void) Applicationdidreceivememorywarning: (uiapplication *) application47 { NSLog (@ "memorywarning"),}50 @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 and creates a delegate object from Delegateclassname. and assigns the delegate object to the delegate property in the UIApplication 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 "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: The number of parameters that the system or user passed in         argv: The actual parameter that the system or user passed in         1. Creates a UIApplication object 2 based on the third parameter passed in.         Based on the fourth passed in, the agent that created the UIApplication object is generated         3. Set the proxy object that you just created as a proxy for UIApplication         4. Turn on an event loop         */         return Uiapplicationmain (argc, argv, @ "uiapplication", @ "Yyappdelegate");}    }

Code and parameter description for system entry:

ARGC: System or user-passed parameter argv: actual parameter passed in by the system or user 1. Based on the third parameter passed in, Create a UIApplication object 2. Based on the fourth incoming agent that generated the UIApplication object, 3. Set the proxy object that you just created as a proxy for uiapplication 4. Turn on an event loop (which can be understood as a dead loop inside) This time loop is a queue (advanced first The first to add in first how iOS programs start

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-Program startup principle and UIApplication

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.