應用程序掛起、復原與終止— 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章