App生命周期分析

在iOS App中,入口函数并不在根目录下,而是在“Supporting Files”目录的main.m文件的main函数中。这很容易理解,C/C++都是以main为入口。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int main(int argc, char * argv[]) {  
  2.     @autoreleasepool {  
  3.         return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));  
  4.     }  
  5. }  

这个函数比较简单,只是调用了UIApplicationMain方法来启动整个应用程序,前两个参数就是普通C/C++的命令行参数,这里我们可以忽略。主要看后面两个参数。后两个参数分别表示程序的主要类(principal class)和app代理类(delegate class)。如果主要类(principal class)为nil,将从Info.plist中获取,如果Info.plist中不存在对应的key,则默认为UIApplication;App代理类(delegate class)将在新建工程时创建,即AppDelegate,应用程序的整个生命周期都由它来代理。

APP生命周期

       根据UIApplicationMain函数,程序将进入AppDelegate.m,这个文件是xcode新建工程时自动生成的。下面看一下AppDelegate.m文件,这个关乎着应用程序的生命周期。

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "AppDelegate.h"  
  2. @interface AppDelegate ()  
  3. @end  
  4.   
  5. @implementation AppDelegate  
  6. // 应用程序第一次启动时执行该函数,如果是手写代码设置应用程序window的rootViewController那么则需要在这里实现。该函数的功能等同于Android中的onCreate函数。  
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  8.     // Override point for customization after application launch.  
  9.     return YES;  
  10. }  
  11. // 应用程序由激活状态切换到未激活状态要执行的函数,例如用户按home键返回主屏幕等。类似于Android中的onPause回调函数  
  12. - (void)applicationWillResignActive:(UIApplication *)application {  
  13.     // 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.  
  14.     // 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.  
  15. }  
  16. // 应用程序已进入后台程序时的回调函数,类似于Android中的onStop  
  17. - (void)applicationDidEnterBackground:(UIApplication *)application {  
  18.     // 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.  
  19.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  20. }  
  21. // 应用程序从未激活状态进入到激活状态要执行的回调函数,过程与WillResignActive相反,等同于Android中的onRestart函数。  
  22. - (void)applicationWillEnterForeground:(UIApplication *)application {  
  23.     // 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.  
  24. }  
  25. // 应用程序被激活的回调,与didEnterBackground过程想对应。onResume  
  26. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  27.     // 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.  
  28. }  
  29. // 应用程序即将终止的回调函数  
  30. - (void)applicationWillTerminate:(UIApplication *)application {  
  31.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  32. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值