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程序启动过程的更多相关文章

  1. 深入理解UIApplication和ios程序启动过程

    在深入理解UIApplication前我们先了解ios程序的启动过程: UIApplication类在ios里面为app的管理和协调提供一个集中的点,每一个app有一个UIApplication的实例 ...

  2. iOS程序启动过程笔记

    CHENYILONG Blog 笔记 一.iOS程序的完整启动过程(有storyboard)1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicati ...

  3. ios程序启动过程和UIWidnow介绍

    一.iOS程序的完整启动过程(有storyboard) 1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情: ...

  4. iOS程序 # 启动过程

    [ App 应用 ] 中文名:缺省 外文名:default 拼音:quē shěng 释义:系统默认状态 全称:缺省状态 -------------- 1.程序启动顺序 1> main.m程序入 ...

  5. iOS程序启动的过程及原理

    iOS程序启动的过程及原理 文字部分 先执行main函数,main内部会调用UIApplicationMain函数 UIApplicationMain函数里面做了什么事情??? 1> 创建UIA ...

  6. IOS程序启动的过程

    IOS程序启动按照以下5个步骤执行 1.main函数 IOS程序启动首先执行main函数 2.UIApplicationMain 执行main函数中的UIApplicationMain函数,这个函数会 ...

  7. IOS程序启动原理

    1.Info.plist 建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 常见属性(红色 ...

  8. Envoy 源码分析--程序启动过程

    目录 Envoy 源码分析--程序启动过程 初始化 main 入口 MainCommon 初始化 服务 InstanceImpl 初始化 启动 main 启动入口 服务启动流程 LDS 服务启动流程 ...

  9. iOS 程序启动流程

    iOS程序启动原理   技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong   iOS应用程序运行 ...

随机推荐

  1. Spring Security3中的-authentication-manager标签详解

    讲解完http标签的解析过程,authentication-manager标签解析部分就很容易理解了 authentication-manager标签在spring的配置文件中的定义一般如下 < ...

  2. Python 安装Twisted 提示python version 2.7 required,which was not found in the registry

    由于我安装Python64位的,下载后没注册,安装Twisted时老提示“python version 2.7 required,which was not found in the registry ...

  3. Node聊天程序实例02:chat_server.js

    作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. chat_s ...

  4. Spring MVC简介

    Spring MVC简介 Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  5. JQuery_处理元素和浏览器的窗口的尺寸

    一.jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包括内边距.边框或 ...

  6. Python_Day9_Socket编程

    本节内容: Socket语法及相关 SocketServer实现多并发 socket概念 socket本质上就是在2台网络互通的电脑之间,架设一个通道,两台电脑通过这个通道来实现数据的互相传递. 我们 ...

  7. Mysql 选择合适的数据类型

    一. char 与 varchar char : 长度固定,所以处理的速度比 varchar 快,但浪费储存空间. varchar : 长度可变,列性能较好.并且平均占用空间少于 char. 因此,选 ...

  8. flume原理及代码实现

    转载标明出处:http://www.cnblogs.com/adealjason/p/6240122.html 最近想玩一下流计算,先看了flume的实现原理及源码 源码可以去apache 官网下载 ...

  9. SQLserver2008数据库备份和还原问题(还原是必须有完整备份)

    首先,我要说明的是你必须拥有完整的数据库备份,下面的还原教程,才算有用,如果其它问题,请搜索别的大牛的解决办法,本方法只适合菜鸟. 这个连接是站长大人的异常恢复方法,有问题可以自己学习http://w ...

  10. IIS 常见问题集记录

    win7 iis7.5 详细错误信息模块 IIS Web Core 通知 BeginRequest 处理程序 尚未确定 错误代码 0x80070021 配置错误 不能在此路径中使用此配置节.如果在父级 ...