在构建一个APP时系统会默认将window的首页指向默认的ViewController
,并且在storyboard
中勾选Is Initial View Controller
。
但是在实际项目中会有navigation
或者tabBar
很少会只有一个viewController
的情况。
那就会出现三种加载情况
- 直接在
storyboard
中将指向需要加载的viewController
- 在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
修改window.rootViewControlle
r。(可以不创建window
对象) - 去掉
Is Initial View Controller
选项,创建self.window
采用window.rootViewController
方式加载。
针对以上三种情况做了一个小Demo实验加载时间。
情况一
直接在storyboard中将指向需要加载的VC,- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
不写任何代码
测试数据
Total pre-main time: 922.79 milliseconds (100.0%)
Total pre-main time: 1.2 seconds (100.0%)
Total pre-main time: 804.45 milliseconds (100.0%)
Total pre-main time: 794.64 milliseconds (100.0%)
Total pre-main time: 293.62 milliseconds (100.0%)
Total pre-main time: 532.53 milliseconds (100.0%)
Total pre-main time: 284.56 milliseconds (100.0%)
Total pre-main time: 523.57 milliseconds (100.0%)
Total pre-main time: 276.15 milliseconds (100.0%)
Total pre-main time: 510.96 milliseconds (100.0%)
最高:1.2
最低:276
平均:614.327
会加载- (instancetype)initWithCoder:(NSCoder *)aDecoder
情况二
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
修改window.rootViewController。
self.window.rootViewController = [[ViewController alloc] init];
测试数据
Total pre-main time: 554.46 milliseconds (100.0%)
Total pre-main time: 346.90 milliseconds (100.0%)
Total pre-main time: 670.21 milliseconds (100.0%)
Total pre-main time: 618.56 milliseconds (100.0%)
Total pre-main time: 339.39 milliseconds (100.0%)
Total pre-main time: 668.87 milliseconds (100.0%)
Total pre-main time: 412.73 milliseconds (100.0%)
Total pre-main time: 643.38 milliseconds (100.0%)
Total pre-main time: 360.34 milliseconds (100.0%)
Total pre-main time: 456.20 milliseconds (100.0%)
最高:670
最低:339
平均:507.104
会加载- (instancetype)initWithCoder:(NSCoder *)aDecoder
和
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
情况三
去掉Is Initial View Controller
选项,创建self.window采用window.rootViewController
方式加载。
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
测试数据
Total pre-main time: 617.35 milliseconds (100.0%)
Total pre-main time: 293.30 milliseconds (100.0%)
Total pre-main time: 642.83 milliseconds (100.0%)
Total pre-main time: 276.14 milliseconds (100.0%)
Total pre-main time: 567.95 milliseconds (100.0%)
Total pre-main time: 570.26 milliseconds (100.0%)
Total pre-main time: 279.53 milliseconds (100.0%)
Total pre-main time: 476.20 milliseconds (100.0%)
Total pre-main time: 649.68 milliseconds (100.0%)
Total pre-main time: 423.90 milliseconds (100.0%)
最高:649
最低:276
平均:479.714
会加载
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
报警告
[Application] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
总结:
目前只是一个Demo 代码量也不多,而且也考虑的不够严谨,需要结合实际项目才能有更精确的结论(Demo 中启动时间差异较大)。是否能提升APP启动速度需要在项目中尝试。
使用工具:
10.14.3 (18D109)
MacBook Pro (13-inch, 2017, Two Thunderbolt 3 ports)
2.3 GHz Intel Core i5
8 GB 2133 MHz LPDDR3
Intel Iris Plus Graphics 640 1536 MB
Xcode Version 10.2 (10E125)
Simulator iPhoine X iOS 12.2
Debug 环境