IOS app startup principle and ios app Principle

Source: Internet
Author: User

IOS app startup principle and ios app Principle

I recently watched a video to learn about the startup principle of the application. Here I will share a blog with you and discuss it with you. If you have any additional or different comments, I can raise them!

1. program entry

As we all know, the entry of an application is generally a main function, and iOS is no exception. In the project's Supporting Files folder, you can find main. m, which is the entry of the program.

Code:

int main(int argc, char * argv[]) {    @autoreleasepool {        return UIApplicationMain(argc, argv, nil,NSStringFromClass([AppDelegate class]));    }}
2. UIApplicationMain function Parameter Parsing

The Declaration of the UIApplicationMain function:

// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);
  • The first and second parameters, argc, and argv, are values in C language.

  • The third parameter, principalClassName, is the main program class name. It can be seen from an English note that when the value is nil, the default value is UIApplication.

  • The fourth parameter: delegateClassName: the proxy class name. In iOS, It is the AppDelegate created along with the project by default.

The fourth parameter is a string, but the main function is NSStringFromClass ([AppDelegate class]). The NSStringFromClass () function is a special conversion function that converts a class name to a string. The [AppDelegate class] function is the class name of the AppDelegate object obtained through reflection.

A series of function declarations similar to NSStringFromClass () functions:

Functions * transform (SEL aSelector); // function name to string FOUNDATION_EXPORT SEL NSSelectorFromString (NSString * aSelectorName); // string to indicate multiple FOUNDATION_EXPORT NSString * NSStringFromClass (Class aClass ); // Class name to string FOUNDATION_EXPORT Class _ nullable NSClassFromString (NSString * aClassName); // string to Class name FOUNDATION_EXPORT NSString * transform (Protocol * proto) NS_AVAILABLE (10_5, 2_0 ); // Protocol name to string FOUNDATION_EXPORT Protocol * _ nullable NSProtocolFromString (NSString * namestr) NS_AVAILABLE (10_5, 2_0); // string to Protocol name
3. Underlying Implementation of UIApplicationMain

(1) create a UIApplication object based on the class name provided by principalClassName

(2) create a UIApplicationDelegate object and become a proxy for the UIApplication object. app. delete = delegate

(3) Enable a main running loop to process events and keep the program running all the time.

(4) Load info. plist and determine whether the Main Interface has a specified main. storyboard. If it is specified, it will load

(5) If specified, load the things that main. stroyboard does.

In the create window, the proxy method in AppDelegate is executed.

Load main. storyboard and load the Controller specified by main. storyboard

Use the newly created controller as the window controller and display the window.

(6) If this parameter is not specified, create a UIWindow object in the proxy method (BOOL) application: didfinishlaunchingwitexceptions: Of AppDelegate as the main window.

Code:

// When the program is started,-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. // 1. create a window. Note that the window must have the same size as the screen size. Do not release the window self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; self. window. backgroundColor = [UIColor redColor]; // 2. create the window's root controller UIViewController * vc = [[UIViewController alloc] init]; vc. view. backgroundColor = [UIColor yellowColor]; [vc. view addSubview: [UIButton buttonWithType: UIButtonTypeContactAdd]; // if you set the window and controller, the view of the controller is added to the window by default. // you can set the window and controller, by default, the rotation function self is available. window. rootViewController = vc; // 3. display window [self. window makeKeyAndVisible]; return YES ;}

Illustration: Illustration of the official help document:

 

Related Article

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.