APP的生命周期关键的是要知道你的应用程序是否正在前台或后台运行。由于系统资源在iOS 设备上较为有限,一个应用程序必须在后台与前台有不同的行为。操作系统也会限制你的应用程序在后台的运行,以提高电池寿命,并提高用户与前台应用程序的体验。同时对于开发人员来说,熟悉APP在任何时候处于什么样的状态也至关重要,熟悉APP所处的状态才能在合适的时机处理相应的事情。
下面就让我们一起来看一下APP的整个生命周期都发了些什么是吧~
一、应用程序的状态
Not running未运行:程序没启动。
Inactive未激活:程序在前台运行,不过没有接收到事件。在没有事件处理情况下程序通常停留在这个状态。
Active激活:程序在前台运行而且接收到了事件。这也是前台的一个正常的模式。
Backgroud后台:程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会。
时间到之后会进入挂起状态(Suspended)。有的程序经过特殊的请求后可以长期处于Backgroud状态。
Suspended挂起:程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。
当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。
二、程序运行各个状态时代理的回调函数
1 代理进程启动但还没进入状态保存
1 - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2 {
3 NSLog(@"①代理进程启动但还没进入状态保存");
4 return YES;
5 }
2 代理启动完成程序准备开始运行
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2 {
3 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
4
5 NSLog(@"在这里可以进行大量的操作,一般第三方介入配置等各种操作");
6
7 // Override point for customization after application launch.
8
9 self.window.backgroundColor = [UIColor whiteColor];
10 [self.window makeKeyAndVisible];
11 return YES;
12 }
3 应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如信息电话等
1 - (void)applicationWillResignActive:(UIApplication *)application
2 {
3 // 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.
4 // 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.
5 NSLog(@"应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如信息电话等");
6 }
4 应用程序进入活动状态执行
1 - (void)applicationDidBecomeActive:(UIApplication *)application
2 {
3 // 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.
4 NSLog(@"应用程序进入活动状态执行");
5 }
5 程序被推送到后台的时候调用,要设置后台继续运行,在这个函数里面设置即可
1 - (void)applicationDidEnterBackground:(UIApplication *)application
2 {
3 // 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.
4 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
5 NSLog(@"要设置后台继续运行,在这个函数里面设置即可");
6
7 [application beginBackgroundTaskWithExpirationHandler:^{
8
9 NSLog(@"begin Background Task With Expiration Handler");
10
11 }];
12 }
6 程序从后台将要重新回到前台时候调用
1 - (void)applicationWillEnterForeground:(UIApplication *)application
2 {
3 // 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.
4 NSLog(@"程序从后台将要重新回到前台时候调用");
5 }
7 程序将要退出时调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值
1 - (void)applicationWillTerminate:(UIApplication *)application
2 {
3 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
4 NSLog(@"程序将要退出时调用");
5 }
8 程序载入后执行
1 - (void)applicationDidFinishLaunching:(UIApplication *)application
2 {
3 NSLog(@"程序载入后执行");
4 }
三、图解各个过程
1 加载应用程序进入前台
2 加载应用程序进入后台
3 基于警告式响应中断
4 进入后台运行
5 返回前台运行
持续更新~🙈