iOS程序运行生命周期
在文件AppDelegate.m中定义了iOS程序运行的各个生命周期,清楚了解程序运行中的每个生命周期,对于我们的开发工作将提供极大的帮助。下面代码就是AppDelegate.m中各生命周期对应的方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"applicationDidBecomeActive");
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"applicationWillTerminate");
}
在上面的代码中,每个生命周期的方法中都加入了NSLog输出,就可以让我们很清楚的知道生命周期的调用时机了。接下来我们对程序运行的各种情况进行分析:
情况1:点击程序icon开始运行(首次运行)
输出日志:
2015-11-10 09:05:27.744 demo010[82333:1062790] didFinishLaunchingWithOptions
2015-11-10 09:05:27.744 demo010[82333:1062790] applicationDidBecomeActive
情况2:程序运行中,按home键
输出日志:
2015-11-10 09:30:53.671 demo010[82333:1062790] applicationWillResignActive
2015-11-10 09:30:54.254 demo010[82333:1062790] applicationDidEnterBackground
情况3:程序运行中按home键之后重新再点击程序icon继续运行
输出日志:
2015-11-10 09:40:42.469 demo010[82333:1062790] applicationWillEnterForeground
2015-11-10 09:40:42.987 demo010[82333:1062790] applicationDidBecomeActive
情况4:程序运行中下拉状态栏,界面显示状态栏信息(如下图1)
输出日志:(
注意:这里只调用applicationWillResignActive!!!)
2015-11-10 09:44:14.655 demo010[82333:1062790] applicationWillResignActive
(图1)
情况5:关闭上图1中的状态栏界面,重新显示程序界面
输出日志:( 注意:这里同样也只调用 applicationDidBecomeActive,与上面情况4对应 !!!)
2015-11-10 09:58:16.066 demo010[82333:1062790] applicationDidBecomeActive
情况6:程序运行中,按电源键(锁屏)
输出日志: (注意:与情况2相同)
2015-11-10 10:02:59.205 demo010[82333:1062790] applicationWillResignActive
2015-11-10 10:02:59.205 demo010[82333:1062790] applicationDidEnterBackground
情况7:在情况6的基础上,再按电源键(解除锁屏)
输出日志: (注意:与情况3相同)
2015-11-10 11:03:08.704 demo010[316:84616] applicationWillEnterForeground
2015-11-10 11:03:09.086 demo010[316:84616] applicationDidBecomeActive
情况8:程序运行中,双击home键(如下图2)
输出日志: (注意:与情况4相同) 2015-11-10 11:13:33.282 demo010[331:87016] applicationWillResignActive
(图2)
情况9:点击上图2中的自己的程序应用(注意是点击,而不是将其上滑删除),以便继续运行。
输出日志: (注意:与情况5相同)
2015-11-10 11:22:22.370 demo010[331:87016] applicationDidBecomeActive
情况10:在上图2的情况下,将程序应用上滑删除。
输出日志:
2015-11-10 11:24:11.935 demo010[331:87016] applicationDidEnterBackground
2015-11-10 11:24:11.980 demo010[331:87016] applicationWillTerminate
标签:11,10,生命周期,程序运行,demo010,iOS,1062790,2015 From: https://blog.51cto.com/u_16160131/6473916