为了研究应用的生命周期,在AppDelegate的方法里面加入打印当前的函数名的方法:

如下:

1、运行程序:

输出:

2、按一下home键

3、再点击应用

4、双击Home键,向上滑动应用,杀掉应用

这个时候控制台没有任何输出,因为你直接杀死应用是不会调用applicationWillTerminate的,只有当应用被系统杀死的时候才会调用这个方法。

5、补充

当收到推送通知的时候会得到以下输出:applicationWillResignActive:

在弹出的通知横幅处向上滑动,让横幅消失,这时会得到以下输出:applicationDidBecomeActive

总结:

// 应用程序的生命周期方法。
// 应用程序完成启动以后调用的方法。
// 在这个方法中我们可以创建要显示的控件。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // 打印当前的函数名,以及当前代码所在文件中得行数// 打印当前的函数名, NSStringFromSelector 获得参数的选择器所代表的方法的字符串
NSLog(@"%@", NSStringFromSelector(_cmd)); // 在这儿加入要显示的控件
// 在应用程序上要显示控件,必须要有窗口,控件的显示都是直接或者间接放到窗口上进行显示, 一般情况下,一个应用程序只有一个窗口。
// 创建窗口对象
self.window = [[UIWindow alloc] init]; // UIScreen 屏幕类 mainScreen取到当前设备的主屏幕 ,bounds取到屏幕的大小
// 设置窗口的大小和坐标
self.window.frame = [[UIScreen mainScreen] bounds]; // 设置窗口的背景颜色为红色
self.window.backgroundColor = [UIColor redColor]; // 让窗口显示
[self.window makeKeyAndVisible];
return YES;
} //后台切换到前台: 后台 --> 不活跃状态 --> 前台(活跃状态)
//前台切换到后台: 前台(活跃状态) --> 不活跃状态 --> 后台 - (void)applicationWillResignActive:(UIApplication *)application{ NSLog(@"%@", NSStringFromSelector(_cmd)); // 方法何时被调用?
// 这个方法在应用程序即将从活跃状态切换到不活跃状态时调用,这种情况会在某些临时中断的事情下发生(比如来电话,来短信等)或者用户退出应用程序时发生。并且应用程序开始转换到后台状态。
// 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. // 在这个方法中,我们可以做啥?
// 使用这个方法,我们可以暂停正在执行的任务,停用定时器,降低OpenGL ES的描绘帧率,游戏应该在这个方法中暂停。
// 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. // 配置应用程序是否能够在后台: 在Info.plist添加一项:Application does not run in background YES 不支持后台运行 NO 支持后台运行 // 当用户退出应用程序时,如果你的应用程序支持后台运行,这个方法会被调用.如果你的应用程序不支持后台运行,applicationWillTerminate:这个方法会被调用。
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. NSLog(@"%@", NSStringFromSelector(_cmd));
} // 应用程序即将进入到前台时调用的方法
- (void)applicationWillEnterForeground:(UIApplication *)application {
// 这个方法作为从后台进入不活跃状态的时候调用的方法。在这儿,你可以撤销在进入后台后做的许多改变。(恢复用户数据) NSLog(@"%@", NSStringFromSelector(_cmd));
} // 应用程序已经成为活跃状态后调用的方法
- (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.
NSLog(@"%@", NSStringFromSelector(_cmd)); } - (void)applicationWillTerminate:(UIApplication *)application {
// 这个方法在应用程序即将终止时调用。在这个方法中保存合适的数据。
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"%@", NSStringFromSelector(_cmd));
} // 应用程序收到内存警告时调用的方法
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
// 释放暂时无用的资源
NSLog(@"%@", NSStringFromSelector(_cmd));
}

iOS 应用的生命周期的更多相关文章

  1. (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解

    原文:http://blog.csdn.net/totogo2010/article/details/8048652 iOS应用程序生命周期(前后台切换,应用的各种状态)详解         分类:  ...

  2. iOS对UIViewController生命周期和属性方法的解析

    目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...

  3. 转:iOS应用程序生命周期(前后台切换,应用的各种状态)详解

    iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: iOS开发进阶2012-10-08 15:35 42691人阅读 评论(30) 收藏 举报 iosapplication任务anima ...

  4. 【iOS开发】iOS对UIViewController生命周期和属性方法的解析

    iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...

  5. iOS - ViewController的生命周期

    iOS SDK中提供很多原生的ViewController,大大提高了我们的开发效率:那么下面我们就根据开发中我们常用的ViewController谈一谈它的生命周期: (一)按照结构和用法可以对iO ...

  6. iOS 应用程序生命周期

    开发应用程序都要了解其生命周期. 今天我们接触一下iOS应用程序的生命周期, iOS的入口在main.m文件: int main(int argc, char * argv[]) { @autorel ...

  7. iOS中的生命周期

    对于一个iOS app来讲,生命周期是一个十分至关重要的东西.对于一个app来讲控制着app的开启.睡眠.关闭等状态:对于一个页面的来讲,控制页面的加载.显示.消失:对于一个View或者一个普通的类来 ...

  8. 2. iOS程序的生命周期

    程序启动-生命周期 来自:  QQ: 853740091 1.首先讲解UIApplication对象 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个 ...

  9. iOS控制器的生命周期分析和使用

    转自http://blog.csdn.net/qijianli/article/details/7826979 iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面 ...

  10. IOS应用程序生命周期的AppDelegate详解

    IOS 中的 AppDelegate.m/h 文件是很重要的呢,因为它是对 Application 的整个生命周期进行管理的. 先明白,每个iPhone应用程序都有一个UIApplication,UI ...

随机推荐

  1. Bower : ENOGIT git is not installed or not in the PATH

    解决方法一: 添加git到window的环境变量中.设置path路径为C:\Program Files\Git\bin 解决方法二: $ set PATH=%PATH%;C:\Program File ...

  2. OpenGL Shader in OpenCASCADE

    OpenGL Shader in OpenCASCADE eryar@163.com Abstract. As implementation of one of the strategic steps ...

  3. JavaScript == 、!=、===、!===的比较

    ; '; ; test == num //true 相同类型 相同值 test === num //true 相同类型 相同值 test !== num //false test与num类型相同,其值 ...

  4. Unity Shaders 第一个默认程序分析

    Unity Shaders 第一个默认程序 Shader "Custom/Shader" { Properties { _MainTex ("Base (RGB)&quo ...

  5. exe4j的使用

    下载:http://download.cnet.com/exe4j/3000-2070_4-144405.html 参考:http://blog.chinaunix.net/uid-25749806- ...

  6. 关于SubSonic3.0生成的表名自动加复数(s)的“用户代码未处理SqlException,对象名'xxxs'无效”异常处理

    使用SubSonic3.0模版生成时,同2.2版本一样,都会自动在一些类似数据库要用到的关键后面加要s(复数),这里也是3.0的一个小Bug,在查询时由于插件并没有完全的去掉s,所以会产生“用户代码未 ...

  7. 由position属性引申的关于css的进阶讨论(包含块、BFC、margin collapse)

    写这篇文章的起因是源于这篇文章:谈谈面试与面试题 中关于position的讨论,文中一开始就说的这句话: 面试的时候问个css的position属性能刷掉一半的人这是啥情况…… 其实这问题我本来打算的 ...

  8. 出操队形(LIS)

    题目来源:微策略2013年校园招聘面试一面试题 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较 ...

  9. LVS+Keepalived搭建MyCAT高可用负载均衡集群

    LVS+Keepalived 介绍 LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国 ...

  10. Hive启动报错: Found class jline.Terminal, but interface was expected

    报错: [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassC ...