iOS程序启动过程
First, the function creates the main application object (step 3 in the flowchart). If you specify
nil
as the third argument toUIApplicationMain()
(the default), it will create an instance ofUIApplication
in this step. This is usually what you want. However, if you need to subclassUIApplication
(for example, to override its event handling insendEvent:
), you have to pass a string with the name of your subclass toUIApplicationMain()
.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 isnil
, though, which signifies that the app delegate will be created in the main NIB file.Next,
UIApplicationMain()
loads and parses your app’sInfo.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).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’sdelegate
outlet (step 6), and aUIWindow
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.
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 causesUIApplicationMain()
to never return.Before the application object processes the first event, it finally sends the well-known
application: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 amakeKeyAndVisible
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程序启动过程的更多相关文章
- 深入理解UIApplication和ios程序启动过程
在深入理解UIApplication前我们先了解ios程序的启动过程: UIApplication类在ios里面为app的管理和协调提供一个集中的点,每一个app有一个UIApplication的实例 ...
- iOS程序启动过程笔记
CHENYILONG Blog 笔记 一.iOS程序的完整启动过程(有storyboard)1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicati ...
- ios程序启动过程和UIWidnow介绍
一.iOS程序的完整启动过程(有storyboard) 1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情: ...
- iOS程序 # 启动过程
[ App 应用 ] 中文名:缺省 外文名:default 拼音:quē shěng 释义:系统默认状态 全称:缺省状态 -------------- 1.程序启动顺序 1> main.m程序入 ...
- iOS程序启动的过程及原理
iOS程序启动的过程及原理 文字部分 先执行main函数,main内部会调用UIApplicationMain函数 UIApplicationMain函数里面做了什么事情??? 1> 创建UIA ...
- IOS程序启动的过程
IOS程序启动按照以下5个步骤执行 1.main函数 IOS程序启动首先执行main函数 2.UIApplicationMain 执行main函数中的UIApplicationMain函数,这个函数会 ...
- IOS程序启动原理
1.Info.plist 建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 常见属性(红色 ...
- Envoy 源码分析--程序启动过程
目录 Envoy 源码分析--程序启动过程 初始化 main 入口 MainCommon 初始化 服务 InstanceImpl 初始化 启动 main 启动入口 服务启动流程 LDS 服务启动流程 ...
- iOS 程序启动流程
iOS程序启动原理 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong iOS应用程序运行 ...
随机推荐
- vnc连接kali 2.0 报错:A problem has occurred and the system can't recover.
kali版本: root@kali:~# uname -a Linux kali -kali1-amd64 # SMP Debian -7kali2 (--) x86_64 GNU/Linux 第一步 ...
- s5pv210 cpu运行debian
1.环境准备 sudo apt-get install binfmt-support qemu qemu-user-static debootstrap 2.获取debian根文件系统 2.1.cd ...
- JFinal 的初始化
浅析初始化过程 首先要从 web 容器进行初始化 <?xml version="1.0" encoding="UTF-8"?> <web-ap ...
- ie6、7下 text-indent 问题
text-indent属性 用于文字缩进,更多是用来隐藏文字.比如,一个logo标题,上面的问题很有艺术感,不得不把文字和背景组合成一张背景图(此处页面元素用a表示),但处于SEO方面的考虑,需要把a ...
- zookeeper 故障重连机制
一.连接多个服务器,用逗号隔开 如果在连接时候zk服务器宕机 To create a client session the application code must provide a connec ...
- Mac安装ctags
Mac安装ctags mac 系统有自带的ctags,但是不支持"ctags -R"指令,需要自己在安装Exuberant Ctags 1.下载ctags 2. 安装 ./conf ...
- django一对多关系的小例题
urls.py from django.conf.urls import urlfrom django.contrib import adminfrom son1.views import * url ...
- Head First 设计模式-- 总结
模式汇总:装饰者 :包装一个对象以得到新的行为状态 :封装了基于状态的行为,并使用委托在行为之间切换迭代器 :在对象的结合中游走,而不暴露集合的实现外观 :简化一群类的接口策略 :封装可以 ...
- RDLC隔行变色的实现
在RDLC设计页面设置行属性中的Background为: =IIF((RowNumber(Nothing)) Mod 2 =1,"Transparent","#F6FAF ...
- Css动画形式弹出遮罩层,内容区上下左右居中于不定宽高的容器中
<!DOCTYPE html> <html> <head> </head> <body id="body"> <! ...