iOS启动过程

Ios应用程序的启动过程

 

由于使用xcode建立项目时,xcode都已经配置好了一系列的模板.这些模板已经配置好了应用程序启动的基本环境.譬如说xcode创建了这样一个对象,他会去连接窗口服务,建立运行循环等等.大部分这类工作都是由UIApplicationMain函数完成的.其定义如下.

 

// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no

// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.

UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);

 

Objective-c的入口函数是main.看下main.m文件

#import <UIKit/UIKit.h>

 

#import “AppDelegate.h”

 

int main(int argc, char *argv[])

{

@autoreleasepool {

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

}

}

 

可以看到UIApplicationMain被调用了.

一般principalClassName是传nil,正如上面注释所说,指定为nil时,会使用UIApplication.

.

UIApplicationMain这个调用做了什么事呢?

1.创建了UIApplication实例

2.创建应用程序代理实例.即上面的AppDelegate

AppDelegate主要功能是提供一个应用程序内容绘制的窗口

3.浏览Info.plist.Info.plist文件提供应用程序的属性列表,如名字,图标等.

 

 

如果应用程序用到了interface builder.比如MainStroy.xib或者MainStroyBoard.storyboard. Info.plist文件还要指定要加载的xib or storyboard的文件名.

 

这里插一下.xib/storyboard是什么哩. 就是压缩文件.在应用程序使用的时候,又还原成代码.如果读者学过其它语言,可以理解成序列化和反序列化.它是用于给用户直接设计界面的,一是减少工程师的工作量,二是做了所见即所得的编辑效果.

应用程序启动时,会根据info.plist 来加载nib/storyboard文件,然后初始化一个viewcontroller实例对象.

 

 

During setup, the application object performs the following tasks:

1.Loads the main storyboard file.

2.Gets the window object from the app delegate (or creates a new instance of UIWindow and associates it with the app delegate).

3.Instantiates the storyboard’s initial view controller and assigns it as the window object’s root view controller.

 

When the application object has completed these tasks, it sends its delegate an application:didFinishLaunchingWithOptions: message. This message gives the delegate an opportunity to perform other tasks, such as additional configuration, before the application is displayed.

 

 

总结一下:

1.入口main

2.调用UIApplicationMain

1.创建了UIApplication实例

2.创建应用程序代理实例.即上面的AppDelegate

AppDelegate主要功能是提供一个应用程序内容绘制的窗口

3.浏览Info.plist.Info.plist文件提供应用程序的属性列表,如名字,图标等.

3.加载xib/storyboard文件

4.从app delegate 获取window对象

5.从xib/storyboard初始化viewcontroller对象,并赋值给window对象的root view controller

6.发送application:didFinishLaunchingWithOptions消息给代理对象.让用户完成自己的一些初始化工作.