【iOS开发-4】UIApplication生命周期以及如何简单测试每个函数何时被调用?

应用程序的生命周期也就是程序在前台和后台以及正在前往后台和前往前台的路上的不同状态,这些不同状态是由不同函数控制的,当然每个函数的作用也不同,比如正在前往后台时可能需要暂停程序进行,进入后台后需要保存一些数据以便再恢复,即将进入前台时需要恢复一些数据,进入前台了可能需要刷新一下用户界面等等。至于详细情况,每一段有个英文解释,耐心看即可。


如何测试呢?思路就是我们模拟进入和退出程序时,系统会调用这些函数(当然这些函数目前里面没内容),我们可以让这些函数把自己的名字打出来,这样我们就能知道我们做什么动作时调用的是哪个函数。_cmd表示当前函数。所以下面每个函数里增加一个打印当前函数即可:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    // Override point for customization after application launch.
    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.
    // 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 {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    // 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 {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    // 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 {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    // 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 {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    NSLog(@"%@",NSStringFromSelector(_cmd));
}

我们按CMD+R进行测试应用程序时,会直接进入程序里,这个时候发现已经调用了2个函数,第一个当然是didFinishLaunchingWithOptions,因为它是程序入口,其次是applicationDidBecomeActive。


然后我们就按CMD+SHIFT+HOME进入主界面发现调用了2个函数,applicationWillResignActive和applicationDidEnterBackground。点击进入应用程序发现也调用了2个函数,applicationWillEnterForeground和applicationDidBecomeActive。这就是我们常用的4个函数。


按CMD+SHIFT+M模拟内存警告发现调用applicationDidReceiveMemoryWarning(这个函数在默认创建的模板中没有,我们手动添加即可)。


但是applicationWillTerminate一直没被调用,原因:这个函数只有在iOS4.0之前用处很大,因为那个时候不能多任务执行,退出一个程序就意味着关闭终止。但现在尽管我们退出一个程序,但它还是在后台并没有关闭,所以这个函数用得比较少。如果实在要用,我们可以在Info.plist中增加一个选项(意思是不让在后台运行,那也就是进入后台即关闭):


这个时候进入后台,我们发现调用了3个函数,其中多了applicationWillTerminate这个函数。


好习惯:测试完记得把上面Info.plist中增加选项删除哦!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值