Application startup Principles in iOS

Source: Internet
Author: User
Tags uikit

Application startup Principles in iOSSource: http://m.warting.com/program/201601/127355.html


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 a new message, such as three-way bar. )

The code is as follows:

@property (nonatomic) Nsinteger applicationiconbadgenumber;
Code implementation and Effects:
-(void) viewdidload
{
[Super Viewdidload];
UIApplication *app=[[uiapplication Alloc]init]; Error, there can be only one unique UIApplication object that can no longer be created
UIApplication *app=[uiapplication Sharedapplication]; Get the UIApplication object for the program by Sharedapplication
app.applicationiconbadgenumber=123;
}

(2) Setting the visibility of the networking indicator

The code is as follows:

@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;
Code and Effects://Set up indicators for networked animations
App.networkactivityindicatorvisible=yes;

(3) Manage status bar

A. Through Uiviewcontroller management (each uiviewcontroller can have its own different status bar)

Starting with IOS7, the system provides 2 ways to manage the status bar, 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 code is as follows: #pragma mark-setting 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 uniformly by it)

If you want to use uiapplication to manage the status bar, you first have to modify the Info.plist settings,

The code is as follows:

Obtain the UIApplication object of the program by Sharedapplication UIApplication *app=[uiapplication Sharedapplication];
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.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
The code is as follows:

-(BOOL) OpenURL: (nsurl*) URL;
OpenURL: Some of the functions of the method are

The phone code is as follows:

UIApplication *app = [Uiapplicationsharedapplication]; [App openurl:[nsurlurlwithstring:@ "tel://10086"];

Send SMS code as follows:

[App openurl:[nsurlurlwithstring:@ "sms://10086"];

Send the email code as follows:

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

Open a page resource code as follows:

[App openurl:[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.

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

The code is as follows:

#import "YYAppDelegate.h"

@implementation Yyappdelegate

Called when the application is finished (automatically called by the system)
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
NSLog (@ "didfinishlaunchingwithoptions");
return YES;
}

Called when it is about to lose its active state (loses focus, cannot interact)
-(void) Applicationwillresignactive: (uiapplication *) application
{
NSLog (@ "resignactive");
}

Regain focus (ability to interact with users)
-(void) Applicationdidbecomeactive: (uiapplication *) application
{
NSLog (@ "becomeactive");
}

Called when the application enters the background
The application's data is typically stored in this method, as well as the state
-(void) Applicationdidenterbackground: (uiapplication *) application
{
NSLog (@ "Background");
}

Called when the application is about to enter the foreground
Typically restores the application's data in this method, as well as the state
-(void) Applicationwillenterforeground: (uiapplication *) application
{
NSLog (@ "Foreground");
}

This method is called when the application is about to be destroyed
Note: This method cannot be called if the application is in a suspended state
-(void) Applicationwillterminate: (uiapplication *) application
{
}

When the application receives a memory warning, it calls the
It is common to release unwanted memory in this method
-(void) applicationdidreceivememorywarning: (uiapplication *) application
{
NSLog (@ "memorywarning");
}
@end

Third, Uiapplicationmain

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


2. The code is as follows: #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]));
Return Uiapplicationmain (argc, argv, @ "uiapplication", @ "Yyappdelegate");
}
}


3. Code and parameter description of system entrance:

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

Iv. complete process of program initiation

1.   have storyboard file  
1. Call the main function  
2. Call the Uiapplicationmain function  
3. Create UIApplication object, Appdelegate object  
4. The proxy that sets the Uiapplicatio object is the Appdelegate object.  
5. The Appdelegate object starts listening for "system events (application events)" and goes to "event loop" &NBSP;
6. Call Application:didfinishlaunchingwithoptions when the program starts: method.  
7. Automatically create   in Application:didfinishlaunchingwithoptions: method;
* UIWindow object. &NBSP
* Find the Storyboard file (Main.storyboard)   that needs to be loaded according to the Info.plist file configuration (Main Interface);
* Locate the Controller class that corresponds to the IS Initial View controller in Main.storyboard, and create the controller object.  
* Create a view for the controller according to the configuration in storyboard.  
* Set UIWindow's root controller (Rootviewcontroller) to the controller you just created.  
* Displays UIWindow ([Self.window makekeyandvisible]).  

2. No Storyboard file
1. Call the main function.
2. Call the Uiapplicationmain function.
3. Creating UIApplication objects, Appdelegate objects
4. The proxy setting for the Uiapplicatio object is the Appdelegate object.
5. The Appdelegate object starts listening for "system events (application events)" and goes to "event loop".
6. Call Application:didfinishlaunchingwithoptions After the program is started: method.
7. Manually create in the Application:didfinishlaunchingwithoptions: method:
* UIWindow
* Controller
* Set UIWindow root controller is the controller you just created
* Show UIWindow

3. iOS program start schematic diagram

4. Four object diagrams

Application startup Principles in iOS

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.