应用程序挂起、复原与终止— IOS开发

一、挂起

当有电话进来或者锁屏,这时你的应用程会挂起,在这时,UIApplicationDelegate委托会收到通知,调用 applicationWillResignActive 方法,你可以重写这个方法,做挂起前的工作,比如关闭网络,保存数据。

C代码   收藏代码
  1. - (void)applicationWillResignActive:(UIApplication*)application{    
  2. /*添加你自己的挂起前准备代码*/    
  3. }    
 

当你的程序被挂起后他不会在后台运行。

 

二、复原

当程序复原时,另一个名为 applicationDidBecomeActive 委托方法会被调用,在此你可以通过之前挂起前保存的数据来恢复你的应用程序:

C代码   收藏代码
  1. - (void)applicationDidBecomeActive:(UIApplication*)application{    
  2. /*添加你的恢复代码*/    
  3. }    
 

注意:应用程序在启动时,在调用了 applicationDidFinishLaunching 方法之后也会调用 applicationDidBecomeActive 方法,所以你要确保你的代码能够分清复原与启动,避免出现逻辑上的bug。

 

三、终止

当用户按下按钮,或者关机,程序都会被终止。当一个程序将要正常终止时会调用 applicationWillTerminate 方法。但是如果长主按钮强制退出,则不会调用该方法。这个方法该执行剩下的清理工作,比如所有的连接都能正常关闭,并在程序退出前执行任何其他的必要的工作:

C代码   收藏代码
  1. - (void)applicationWillTerminate:(UIApplication*)application{    
  2. /*在这里添加退出前的清理代码以及其他工作代码*/    
  3. }    
 

来源: http://blog.csdn.net/iukey/article/details/7311115

 

 

Java代码   收藏代码
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     // Override point for customization after application launch.  
  4.     NSLog(@"\n ===> 程序开始 !");   
  5.       
  6.     return YES;  
  7. }  
  8.                               
  9. - (void)applicationWillResignActive:(UIApplication *)application  
  10. {  
  11.     /* 
  12.      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. 
  13.      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. 
  14.      */  
  15.     NSLog(@"\n ===> 程序暂行 !");   
  16. }  
  17.   
  18. - (void)applicationDidEnterBackground:(UIApplication *)application  
  19. {  
  20.     /* 
  21.      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.  
  22.      If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
  23.      */  
  24.      NSLog(@"\n ===> 程序进入后台 !");   
  25. }  
  26.   
  27. - (void)applicationWillEnterForeground:(UIApplication *)application  
  28. {  
  29.     /* 
  30.      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. 
  31.      */  
  32.      NSLog(@"\n ===> 程序进入前台 !");   
  33. }  
  34.   
  35. - (void)applicationDidBecomeActive:(UIApplication *)application  
  36. {  
  37.     NSLog(@"\n ===> 程序重新激活 !");   
  38.     /* 
  39.      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. 
  40.      */  
  41. }  
  42.   
  43. - (void)applicationWillTerminate:(UIApplication *)application  
  44. {  
  45.     NSLog(@"\n ===> 程序意外暂行 !");   
  46.   
  47.     UIDevice *device = [UIDevice currentDevice];  
  48.     /* 
  49.      Called when the application is about to terminate. 
  50.      Save data if appropriate. 
  51.      See also applicationDidEnterBackground:. 
  52.      */  
  53. }  
 

首次运行

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

- (void)applicationDidBecomeActive:(UIApplication *)application

 

首次关闭(home):

- (void)applicationWillResignActive:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application

 

再次运行:

- (void)applicationWillEnterForeground:(UIApplication *)application

- (void)applicationDidBecomeActive:(UIApplication *)application

 

再次关闭:

- (void)applicationWillResignActive:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application



转自:http://justcoding.iteye.com/blog/1473350

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值