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. EL表达式语言总结

    EL介绍 Expressive Language, JSP2.0引入,简化jsp开发中对对象的引用. 基本语法 ${表达式} 常见用法 根据其访问对象,可大体分成三类:访问数据及其各种表达式,访问EL ...

  2. java内存泄漏的经典案例

    这篇文章主要介绍了Java中典型的内存泄露问题和解决方法,典型的内存泄露例子是一个没有实现hasCode和 equals方法的Key类在HashMap中保存的情况,可以通过实现Key类的equals和 ...

  3. 使用Gson转换json数据为Java对象的一个例子

    记录工作中碰到的一个内容. 原料是微信平台的一个接口json数据. { "errcode" : 0, "errmsg" : "ok", &q ...

  4. HTML5 Canvas一些常用的操作

    粗略的Canvas API 1. context var context = canvas.getContext('2d'); 2.Canvas state context.save();//将当前状 ...

  5. Linux-ubuntu指令使用积累(长期更新)

    alias cat cd cp ls mkdir mv rm sudo tar chmod       1. sudo 系统管理指令.放在其它指令之前使用,允许普通用户在root权限下执行部分或者全部 ...

  6. node与socket.io搭配小例子-转载

    //服务端代码 io = require('socket.io').listen(app), fs = require('fs'), cookie=require('cookie'); request ...

  7. Python_Day5_迭代器、装饰器、软件开发规范

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 1.列表生成式,迭代器&生成器 列表生成 >>> a = [i+1 ...

  8. Win7全自动精简批处理_温柔处理极速修正版/暴力剩女工程测试版

    2011htpcfans 发表于 2012-5-11 http://bbs.wuyou.net/forum.php?mod=viewthread&tid=210269&highligh ...

  9. //解决validator验证插件多个name相同只验证第一的问题

    //解决validator验证插件多个name相同只验证第一的问题 var validatorName = function () { if ($.validator) { $.validator.p ...

  10. Numpy Study 1

    Numpy 使用1 1.Numpy创建数组 import numpy as np 创建数组有以下方式: (1).arange numpy.arange([start, ]stop, [step, ]d ...