iOS程序启动过程

iOS程序启动过程

  1. First, the function creates the main application object (step 3 in the flowchart). If you specify nil as the third argument to UIApplicationMain() (the default), it will create an instance of UIApplication in this step. This is usually what you want. However, if you need to subclass UIApplication (for example, to override its event handling insendEvent:), you have to pass a string with the name of your subclass toUIApplicationMain().

  2. The function then looks at its fourth argument. If it is non-nil, it interprets it as the name of the class for the application delegate, instantiates an object of this class and assigns it as the application object’s delegate. The default for the fourth argument is nil, though, which signifies that the app delegate will be created in the main NIB file.

  3. Next, UIApplicationMain() loads and parses your app’s Info.plist (step 4). If it contains a key named “Main nib file base name” (NSMainNibFile), the function will also load the NIB file specified there (step 5).

  4. By default, the main NIB file is called MainWindow.nib. It contains at least an object representing the application delegate, connected to the File’s Owner’s delegate outlet (step 6), and a UIWindow object that will be used as the app’s main window, connected to an outlet of the app delegate. If you used a view-controller-based app template, the NIB file will also contain your app’s root view controller and possibly one or more view child controllers.

    It is worth mentioning that this is the only step where the UIKit-based app templates (Window-based, View-based, Navigation-based, Tab-based, etc.) differ significantly from each other. If you started out with a view-based app and later want to introduce a navigation controller, there is no need to start a new project: simply replace the root view controller in the main NIB file and adjust one or two lines of code in the app delegate. I noticed that many newbies to the iOS platform struggle with this problem and assume a huge difference between the different project templates. There isn’t.

  5. Now, UIApplicationMain() creates the application’s run loop that is used by theUIApplication instance to process events such as touches or network events (step 7). The run loop is basically an infinite loop that causes UIApplicationMain() to never return.

  6. Before the application object processes the first event, it finally sends the well-knownapplication:didFinishLaunchingWithOptions: message to its delegate, giving us the chance to do our own setup (step 8). The least we have to do here is put our main window on the screen by sending it a makeKeyAndVisible message.

// 这是原文我摘抄的部分,英语好的可以看看,下面说说自己的理解

1 main函数

int main(int argc, char * argv[]) {
NSLog(@"===%s",argv[]);
@autoreleasepool {
/// 函数原型:
// int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

这里面的代码一般是不变的。首先是一个自动释放池,保证结束时内存释放,下面是参数介绍:

  argc, argv:是标C的参数,argc为argv数组中元素的个数。argv一般有一个元素argv[0]即当前可执行程序的路径。(另外,在linux系统下我们通过终端打开一个程序可以给它传递参数,具体不再展开。如果不知所云括号里面的自动忽略);

2 UIApplicationMain

   1)根据传进的参数创建UIApplication对象;

   2)根据传进的参数创建UIApplication的delegate对象,并将该delegate对象赋值给UIApplication对象中的delegate属性。

   3)开启一个消息循环

具体阐述:

main函数中执行了一个UIApplicationMain这个函数

int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); 
argc、argv:直接传递给UIApplicationMain进行相关处理即可

principalClassName:指定应用程序类名(app的象征),该类必须是UIApplication(或子类)。如果为nil,则用UIApplication类作为默认值,它是一个单例,代表一个进程,也是程序创建的第一个对象,利用UIApplication对象,能进行一些应用级别的操作;

delegateClassName:指定应用程序的代理类,该类必须遵守UIApplicationDelegate协议

UIApplicationMain函数会根据principalClassName创建UIApplication对象,根据delegateClassName创建一个delegate对象,并将该delegate对象赋值给UIApplication对象中的delegate属性

接着会建立应用程序的Main Runloop(事件循环),进行事件的处理(首先会在程序完毕后调用delegate对象的application:didFinishLaunchingWithOptions:方法)

程序正常退出时UIApplicationMain函数才返回

下面分为storyboard启动和没有storyboard启动

##有storyboard##

3 根据Info.plist加载storyboard

   1)创建UIWindow,UIWindow是一种特殊的UIView,通常在一个App中只会有一个UIWindow(注意是通常,还有其它的,比如弹出的键盘)。设置为主窗口,同一时刻主窗口只有一个,可以通过[UIApplication sharedApplication].keyWindow获取。

   2)创建和设置UIWindow的rootViewController。

   3)显示窗口

具体阐述:

根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard)

- 创建UIWindow 
- 创建和设置UIWindow的rootViewController(storyboard中箭头指向的控制器) 
- 显示窗口 
有storyboard:官方文档:iosX /uikit/viewController p c for ios
The Main Storyboard Initializes Your App’s User Interface 
The main storyboard is defined in the app’s Information property list file. If a main storyboard is declared in this file, then when your app launches, iOS performs the following steps:

    • It instantiates a window for you.
    • It loads the main storyboard and instantiates its initial view controller.
    • It assigns the new view controller to the window’s rootViewController property and then makes the window visible on the screen.

翻译:

      • 主故事面板初始化应用程序的用户界面
        主故事面板中定义的应用程序的信息属性列表文件。如果一个主要的故事是在这个info.plist文件中声明(main),然后当你的应用程序启动,iOS执行以下步骤:
        1.它为你实例化一个窗口。
        2.它加载(箭头所指向)主故事面板并实例化为初始视图控制器。
        3.它赋予新的视图控制器窗口为RootViewController并将在屏幕上显示这个窗口内容。

##没有storyboard##

3 delegate对象开始处理(监听)系统事件(没有storyboard)

   1)程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法

2)在application:didFinishLaunchingWithOptions:中创建UIWindow:window

3)创建和设置UIWindow的rootViewController

4)显示并设置window为主窗口:[window makeKeyAndVisible]; 然后self.window = window;防止被释放。

iOS程序启动过程

上一篇:【SSH进阶之路】Hibernate映射——多对一单向关联映射(四)


下一篇:关于 HTML5 的 11 个让人难以接受的事实