夯实基础 砥砺前行 --- UIApplication(中)

UIApplication和delegate

  • 所有的移动操作系统都有个致命的缺点:app很容易受到打扰。比如一个来电或者是锁屏会导致app进入后台甚至终止。
  • 还有很多其他类似的情况都会导致app受到干扰,app受到干扰时,会产生一系列系统事件,这时UIApplication会通知他的delegate对象,让delegate来处理系统事件

delegate可处理的事件包括:

  • 应用程序的生命周期事件(如程序的启动和关闭)
  • 系统事件(如来电)
  • 内存警告
UIApplication和delegate

delegate方法讲解:

//应用程序启动完成后就会调用AppDelegate方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}
//当应用程序失去焦点的时候调用,只有当应用程序完全获取焦点的时候才能够与用户交互,所谓的获取焦点我的理解为app启动布满屏幕。
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
//当应用程序进入后台时候调用
- (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.
}
//当应用程序关闭的时候调用
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

各方法的调用时机的演示以及讲解
1.程序一启动未做任何操作
程序加载完毕,并获得焦点


程序启动

2.点击home键进入手机主界面
application先失去焦点然后 进入后台


程序进入后台

3.点击应用重新进入
application进入前台然后获得焦点


程序进入前台

4.双击home键关闭程序
程序关闭
关闭程序

由控制台打印结果可知按照上面的操作,先后顺序为:
程序加载完毕-->程序获得焦点-->程序失去焦点-->程序进入后台-->程序进入前台-->程序获得焦点-->程序关闭

程序的启动流程

讲解

演示main以及控制台打印结果

查看官方文档,了解参数的介绍
第三个参数:UIApplication类名或者子类的名称,如果传nil的话默认为@“UIApplication”
第四个参数:UIApplication的代理的类型名称,苹果使用NSStringFromClass([AppDelegate class])方法,获得对应类名,使用该方法除了可以避免输入错误的同时还具有提示功能。


官方文档

UIApplication底层实现原理:

1.根据principalClassName(第三个参数)传递的类名创建一个UIApplication对象
2.创建UIApplication代理对象,给UIApplication对象设置代理
3.开启主运行事件循环,处理事件,保持程序一直运行(后期runloop讲解)
4.加载Info.plist,判断application的Info.pist文件是否指定main,如果指定main就会记载nib文件

iOS程序的启动过程流程图

流程图

你可能感兴趣的:(夯实基础 砥砺前行 --- UIApplication(中))