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. Xcode 7免证书真机调试

    在Xcode 7中,苹果改变了自己在许可权限上的策略,此前Xcode只开放给注册开发者下载,但Xcode 7改变了这种惯有的做法,无需注册开发者账号,仅使用普通的Apple ID就能下载和上手体验.此 ...

  2. virtualbox安装增强功能时【未能加载虚拟光盘】

    virtualbox安装增强功能时[未能加载虚拟光盘] 今天在使用Virtualbox中的Ubuntu虚拟机,想安装增强功能来实现更改分辨率,但是在安装时出错:未能加载虚拟光驱 VBoxsGuestA ...

  3. LaTeX用dvi编译,Yap浏览器弹出对话框,决解办法(傻瓜教程)

      1,打开windows-----所有运用,找到CTEX的文件目录 2,打开Previewer对话框 打开后如图: 2,选择view – option 打开后如下图: 3,选择Display---- ...

  4. T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

    -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316-- ...

  5. C++学习注意点

    1.cin,cout关同步再用,不然效率很糟cin,cout关同步再用,不然效率很糟cin,cout关同步再用,不然效率很糟.重要的事情说三遍.关同步代码:std::ios::sync_with_st ...

  6. YYYY-mm-dd HH:MM:SS

    备忘:YYYY-mm-dd HH:MM:SS部分解释 d               月中的某一天.一位数的日期没有前导零.    dd             月中的某一天.一位数的日期有一个前导零 ...

  7. Redis setNX 实现分布式锁(重复数据插入可用其来实现排他锁)

    使用Redis的 SETNX 命令可以实现分布式锁,下文介绍其实现方法. SETNX命令简介 命令格式 SETNX key value 将 key 的值设为 value,当且仅当 key 不存在. 若 ...

  8. ROS学习笔记(一)——软件版本的选择

    下面是Google的SLAM系统Cartographer对系统的要求: Cartographer对ROS版本要求: ROS Indigo 对Ubantu 的版本要求: 所以,综上所述: Ubantu ...

  9. tcp 之失败重传机制

    1.回退N步协议: 滑动窗口模式,每次传送一批的数据,接收到一个就再放进去一个,如果前面一个没有收到回复,后面的就算收到了后面的数据也丢掉 2选择性重传 区别是收到后,缓存.

  10. Linux 虚拟机重新生成配置文件无法启动

    ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting down loopback insterface:  ...