iOS Development--ui Advanced Chapter (vii) program start principle

Source: Internet
Author: User

I. Common settings for Info.plist

1, the establishment of a project, the supporting files folder will see a "project name-info.plist" file, the file on the project to do some run-time configuration, very important, can not be deleted

In the project created by the older version of Xcode, the profile name is called "Info.plist"

Other plist files in the project cannot have the word "Info", or they will be mistaken for the "info.plist" that is very important in the legend.

There is also a infoplist.strings file in the project that is related to localization of info.plist files

2. Common attributes (red is the key you see when opened with a text editor)
Localiztion native Development Region (cfbundledevelopmentregion)-Localization related

Bundle Display Name (cfbundledisplayname)-a program that displays the names after installation, limited to 10-12 characters, and if exceeded, will be displayed abbreviated names

Icon file (cfbundleiconfile)-app icons name, typically icon.png

Bundle version (cfbundleversion)-The version number of the application, which needs to be increased each time a new version is posted to the App Store

Main Storyboard file base name (nsmainstoryboardfile)-the name of the primary storyboard

Bundle identifier (cfbundleidentifier)-Unique identification of the project, deployed to the real machine


3. pch file

PCH is also a header file, compiled in advance
Build Setting-Prefix
PCH Effect:

1. Store some common macros
2. Store some common header files
3. Custom Log

PCH principle: Is to copy the contents of the PCH file to the head of all files

Note the point:
PCH file must pay attention to make some judgments, determine whether the OC file, if it is to import OC code

. pch

//The following macros should be imported in the OC file//all OC files will be defined __OBJC__ This macro, Apple defines#ifdef__objc__//get the current version of the phone system#defineIOS8 ([[Uidevice currentdevice].systemversion Floatvalue] >= 8.0)#import "uiimage+image.h"//Custom Log// ... Represents a variable parameter inside a macro//__va_args__ represents a variable parameter inside a function#ifdefDEBUG//represents the current debugging phase#defineXmglog (...) NSLog (__va_args__)#else //Release Phase#defineXmglog (...)#endif#endif

Second, uiapplication

1. What is UIApplication
UIApplication objects are symbols of the application

Each application has its own UIApplication object, and it is a singleton

This singleton object can be obtained by [UIApplication Sharedapplication]

The first object created after an iOS program is started is a UIApplication object

Use the UIApplication object to perform some application-level operations


2. Common Properties of UIApplication

Set the red reminder number in the upper-right corner of the application icon
@property (nonatomic) Nsinteger applicationiconbadgenumber;

Setting the visibility of networking indicators
@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;

3. Status bar in iOS

1) starting from IOS7, the system provides 2 ways to manage the status bar
Through Uiviewcontroller management (each uiviewcontroller can have its own different status bar)
Managed by UIApplication (the status bar of an application is unified by it)

2) 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

Style of the status bar
-(Uistatusbarstyle) Preferredstatusbarstyle;

Visibility of the status bar
-(BOOL) Prefersstatusbarhidden;


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

4) UIApplication has a very powerful OpenURL: method
-(BOOL) OpenURL: (nsurl*) URL;

OpenURL: Some of the functions of the method are

//CallUIApplication *app =[UIApplication sharedapplication]; [App Openurl:[nsurl urlwithstring:@"tel://10086"]];//Send SMS[App Openurl:[nsurl urlwithstring:@"sms://10086"]];//send e-mail[App Openurl:[nsurl urlwithstring:@"Mailto://[email protected]"]];//open a Web resource[App Openurl:[nsurl urlwithstring:@"http://ios.itcast.cn"]];

5) UIApplication single case

The singleton is that there can be only one instance object, no matter how it is created
If the address is the same, it represents the same instance object
In general, creating a singleton object has a class method corresponding to it
In general, the method names used to create the Singleton object start with share or start with default

Get the symbol of the app
UIApplication *app = [UIApplication sharedapplication];

The following mimics the Apple's internal principles to write a singleton object

#import<Foundation/Foundation.h>@interfacePerson:nsobject+(instancetype) Sharedperson;@end#import "Person.h"@implementation PersonStaticPerson *p =Nil;//when the program starts, a person is created immediately//How do I know when the program starts?//function: Load class to Memory//When to call: The program loads classes into memory when it starts//is only called once, and the class only needs to be loaded once+ (void) load{//Create Person Object[self Sharedperson];}+(instancetype) sharedperson{//something inside a code block executes only once .    Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{NSLog (@"dispatch_once"); P=[[Self alloc] init];        }); //Create Person Object    returnp;}+(instancetype) alloc{//you don't need to throw an exception for the first time    if(p) {//Throw Exception//Name: Names of exceptions//reason: The cause of the exceptionNSException *exce = [NSException exceptionwithname:@"nsinternalinconsistencyexception"Reason@"There can is only a person instance."Userinfo:nil];    [Exce raise]; }        return[Super alloc];}@end

Iii. UIApplication and delegate

1, all mobile operating systems have a fatal disadvantage: The app is very vulnerable to be disturbed. 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 the delegate agent to 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)
Memory warning

2. Uiapplicationdelegate Agreement

// The app receives a memory warning when called -(void) applicationdidreceivememorywarning: (uiapplication *) application; // when the app enters the background (such as by pressing the home button)-(void) Applicationdidenterbackground: (uiapplication *) application; // when the app is started, call -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions;

3, Uiapplicationdelegate
Each new project, there is a "appdelegate" word of the class, it is the agent of UIApplication
The Uiapplicationdelegate protocol has been adhered to by default and is already a proxy for uiapplication

Iv. boot process for iOS programs

Uiapplicationmain


A uiapplicationmain function is executed in the main function.

int uiapplicationmain (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

Wu, UIWindow

1, UIWindow is a special kind of uiview, usually in an app will have at least one UIWindow

After the iOS program starts, the first view control created is UIWindow, then the controller's view is created, the controller's view is added to the UIWindow, and the controller's view is displayed on the screen.

An iOS program can be displayed on the screen entirely because it has UIWindow

It says that without UIWindow, you can't see any UI interface.

2. Add UIView to UIWindow in two common ways:
-(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, the acquisition of UIWindow
[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

The relationship between four objects

iOS Development--ui Advanced Chapter (vii) program start principle

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.