1. iOS app launch sample diagram:
2, the application start principle: 2-1, does not start through the storyboard file:
- Executes the main function;
- Executes the Uiapplicationmain function in the main function, creates the UIApplication object, creates the Applicationdelegate object, and sets it as the proxy for the UIApplication object;
- The program starts, turns on the message loop (Main runloop), listens for events and invokes the method of registration, and then invokes the agent's
application:didFinishLaunchingWithOptions:
methods; In this method, manually create the UIWindow object set to the Proxy window property;
- Create the controller and set the controller to the window's root controller (Rootviewcontroller), at which point the view in the controller is automatically loaded onto the window;
- Set the created window to be the primary and display (the method that called Window
makeKeyAndVisible
).
Sample Program :
1./**
2. 应用程序启动完毕
3. */
4.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
5. // 1. 创建窗口
6. UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
7. self.window = window;
8.
9. // 2. 创建窗口的根控制器
10. UIViewController *vc = [[UIViewController alloc] init];
11. vc.view.backgroundColor = [UIColor redColor];
12. self.window.rootViewController = vc;
13.
14. // 3. 成为主窗口并显示
15. [self.window makeKeyAndVisible];
16.
17. return YES;
18.}
2-2. Start by Storyboard file: Official document Explanation:
- Executes the main function;
- Executes the Uiapplicationmain function in the main function, creates the UIApplication object, creates the Applicationdelegate object, and sets it as the proxy for the UIApplication object;
- Automatically create good UIWindow objects;
- Retrieves the most important storyboard (usually the main storyboard) information in the Info.plist file, loads the storyboard file according to the information provided by the Info.plist, and automatically creates a view of the controller and Controller;
- Set the created controller to the root controller (rootviewcontroller) of the Window object and display the Windows.
- When the application starts, it opens the main Runloop (event loop) of the application, listens for the event and invokes the method of registration (the method that calls the delegate object first after the program is finished
application:didFinishLaunchingWithOptions:
);
3, Uiapplicationmain () function: Official document explanation: 3-1, uiapplicationmain () parameter instruction:
argc
: Use the ARGC in the main function;
argv
: Use the argv of the main function;
principalClassName
: Specifies the application class name (symbol of the app), which must be uiapplication (or subclass) and, if nil, use the UIApplication class as the default value;
备注
: Incoming Application object class name, nil
= = NSStringFromClass([UIApplication class])
= @"UIApplication"
.
delegateClassName
: Specifies the application's proxy class, uiapplicationdelegate the method defined in the protocol, implemented in the class.
3-2, Uiapplicationmain () function:
- Create UIApplication objects based on Principalclassname;
- Creates a delegate object based on Delegateclassname and assigns the delegate object to the delegate attribute in the UIApplication object;
UIApplicationMain()
function to open a "dead loop" (event loop), so the program does not exit, we can use any. In this "Dead Loop" (event loop) The program constantly listens to the user's various events and processes them sequentially (depending on the "event queue" implementation).
备注
: In the message loop to continuously monitor 消息队列
whether there are new events (queue is FIFO, the purpose of the event queue is to improve performance), the message loop can be simply understood as a dead loop, when the system has an event triggered, the method of the event is put into the message queue, the function of the message loop is to keep going to the message There is no new message in the queue, and there is no time for the user to feel the wait.
3-3, Uiapplicationmain () function exit:
备注
: The Uiapplicationmain function returns when the program exits normally.
4, program start four object relationship example diagram:
iOS Core note--ios application startup principle