iOS APP启动流程
看图说话,没图说个xx
未使用storyboard的启动
从上面两张图我们可以看出,也是从main()函数开始,iOS的面函数比较简单就几行代码,只是调用UIApplicationMain函数 ,这个函数如下
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
- argc 表示参数个数
- argv 表示函数参数的数组
- 是UIApplication类名或者是其子类名,如果是nil,则就默认使用UIApplication类名
- 是协议UIApplicationDelegate的实例化对象名,如果是nil,则从main nib文件中加载委托对象。这个对象就是UIApplication对象监听到系统变化的时候通知其执行的相应方法
此函数会创建一个UIApplication对象,并根据第四个参数创建一个delegate对象,然后将此delegate对象赋值给UIApplication对象的delegate属性。
接着会建立应用程序的Main Runloop循环,调用UIApplicationDelegate中的函数进行事件处理
UIApplication
UiApplication代表一个应用程序,每一个应用程序都有一个UIApplication全局对象,而且这个对象是单例的,我们在程序中可以通过[UIApplication sharedApplication]获得这个对象,进行一些应用级的操作。UIApplication是应用程序的开始,它维护了一个在本应用程序中打开的Window列表,负责初始化显示UIWindow,并负责加载应用程序的第一个UIView到UIWindow窗口中,在随后的操作中,我们也可以更换UIWindow窗口的显示内容。此外,UIApplication还被赋予一个代理对象,在实际编程中,我们一般并不直接与UIApplication打交道,而是和其代理对象UIApplicationDelegate打交道,UIApplication负责监听接收事件,而由UIApplicationDelegate决定应用程序如何去响应这些事件(生命周期:程序启动和关闭,系统事件:来电、记事项警告)等等。
UIApplicationDelegate
- Not running:App还没运行
- Inactive:App运行在foreground但没有接收事件
- Active:App运行在foreground和正在接收事件
- Background:运行在background和正在执行代码
- Suspended:运行在background但没有执行代
如图所示,当App启动时,首先由not running状态切换到inactive状态,此时调用application:didFinishLaunchingWithOptions:方法;然后由inactive状态切换到active状态,此时调用applicationDidBecomeActive:方法。
当App发生中断时,由active状态切换到inactive状态,此时调用applicationWillResignActive:方法。
如图所示,当切换到另一个App时,由状态active切换到inactive,此时调用applicationWillResignActive:方法;然后从inactive状态切换到running状态,此时调用applicationDidEnterBackground:方法
-
锁屏
如何所示,当手机锁屏时,由状态active切换到inactive,此时调用applicationWillResignActive:;然后再由inactive状态切换到running状态,此时调用applicationDidEnterBackground:方法
AppDelegate.h/m define a class that manages the application overall. The app will create one instance of that class and send that object messages that let the delegate influence the app's behavior at well-defined times. For example, -application:didFinishLaunchingWithOptions: is sent when the app has finished launching and is ready to do something interesting. Take a look at the UIApplicationDelegate reference page for a list of messages that the app delegate can implement to modify the behavior of the application.
所有的移动操作系统都有个致命的缺点:app很容易受到打扰.比如一个来电或者锁屏会导致app进入后台甚至被终止;还有很多其他类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate来处理这些系统事件.由图二我们可以看到代理类可以处理许多事件
- 程序加载完毕
- 程序获取焦点
- 程序进入后台
- 程序失去焦点
- 程序从后台回到前台
- 内存不足警告
- 程序即将退出
程序中的AppDelegate.m都有相关的对应函数
import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
// 当应用程序启动完毕的时候就会调用(系统自动调用)
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Override point for customization after application launch.
return YES;
}
// 当应用程序从活动状态(active)变到非活动状态(inactive时被触发调用, 这可能发生在一些临时中断下(例如:来电话、来短信)又或者程序退出时,他会先过渡到后台然后terminate 使用这方法去暂停正在进行的任务,禁用计时器,节流OpenGL ES 帧率。在游戏中应该在这个方法里面暂停游戏。
-(void)applicationWillResignActive:(UIApplication *)application {
}
//应用程序进入后台的时候调用
//使用这种方法来释放共享资源,保存用户数据,无效计时器,存储足够多的应用程序状态信息来恢复您的应用程序的当前状态,以防它终止丢失数据。 如果你的程序支持后台运行,那么当用户退出时不会调用applicationWillTerminate。
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
// 应用程序即将进入前台的时候调用
// 一般在该方法中恢复应用程序的数据,以及状态
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
//重新获取焦点(能够和用户交互) 重启所有的任务,不管是从非活动状态还是刚启动程序,还是后台状态。
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
// 应用程序即将被销毁的时候会调用该方法 game over
// 注意:如果应用程序处于挂起状态的时候无法调用该方法
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:
}
ViewController中的加载顺序
pragma mark --- life circle
// 非storyBoard(xib或非xib)都走这个方法
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
NSLog(@"%s", __FUNCTION__);
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}
// 如果连接了串联图storyBoard 走这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder { NSLog(@"%s", __FUNCTION__);
if (self = [super initWithCoder:aDecoder]) {
}
return self;
}
// xib 加载 完成
- (void)awakeFromNib {
[super awakeFromNib];
NSLog(@"%s", __FUNCTION__);
}
// 加载视图(默认从nib)
- (void)loadView {
NSLog(@"%s", __FUNCTION__);
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view.backgroundColor = [UIColor redColor];
}
//视图控制器中的视图加载完成,viewController自带的view加载完成
- (void)viewDidLoad {
NSLog(@"%s", __FUNCTION__);
[super viewDidLoad];
}
//视图将要出现
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"%s", __FUNCTION__);
[super viewWillAppear:animated];
}
// view 即将布局其 Subviews
- (void)viewWillLayoutSubviews {
NSLog(@"%s", __FUNCTION__);
[super viewWillLayoutSubviews];
}
// view 已经布局其 Subviews
- (void)viewDidLayoutSubviews {
NSLog(@"%s", __FUNCTION__);
[super viewDidLayoutSubviews];
}
//视图已经出现
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"%s", __FUNCTION__);
[super viewDidAppear:animated];
}
//视图将要消失
- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"%s", __FUNCTION__);
[super viewWillDisappear:animated];
}
//视图已经消失
- (void)viewDidDisappear:(BOOL)animated {
NSLog(@"%s", __FUNCTION__);
[super viewDidDisappear:animated];
}
//出现内存警告 //模拟内存警告:点击模拟器->hardware-> Simulate Memory Warning
- (void)didReceiveMemoryWarning {
NSLog(@"%s", __FUNCTION__);
[super didReceiveMemoryWarning];
}
// 视图被销毁
- (void)dealloc {
NSLog(@"%s", __FUNCTION__);
}
查看 打印 结果
2017-07-24 19:02:46.879 app启动流程[9037:390823] -[ViewController initWithCoder:]
2017-07-24 19:02:46.880 app启动流程[9037:390823] -[ViewController awakeFromNib]
2017-07-24 19:02:46.883 app启动流程[9037:390823] -[ViewController loadView]
2017-07-24 19:02:46.883 app启动流程[9037:390823] -[ViewController viewDidLoad]
2017-07-24 19:02:46.883 app启动流程[9037:390823] -[ViewController viewWillAppear:]
2017-07-24 19:02:47.027 app启动流程[9037:390823] -[ViewController viewWillLayoutSubviews]
2017-07-24 19:02:47.027 app启动流程[9037:390823] -[ViewController viewDidLayoutSubviews]
2017-07-24 19:02:47.027 app启动流程[9037:390823] -[ViewController viewWillLayoutSubviews]
2017-07-24 19:02:47.027 app启动流程[9037:390823] -[ViewController viewDidLayoutSubviews]
2017-07-24 19:02:47.039 app启动流程[9037:390823] -[ViewController viewDidAppear:]