1. 程式人生 > >IOS applicationWillResignActive掛起

IOS applicationWillResignActive掛起

註意 sdi rri active options 啟動 前臺 lte 清理

一、掛起

當有電話進來或者鎖屏,這時你的應用程會掛起,在這時,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. }

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. NSLog(@"\n ===> 程序暫行 !");
13. }
14.
15. - (void)applicationDidEnterBackground:(UIApplication *)application
16. {
17.
18. NSLog(@"\n ===> 程序進入後臺 !");
19. }
20.
21. - (void)applicationWillEnterForeground:(UIApplication *)application
22. {
23.
24. NSLog(@"\n ===> 程序進入前臺 !");
25. }
26.
27. - (void)applicationDidBecomeActive:(UIApplication *)application
28. {
29. NSLog(@"\n ===> 程序重新激活 !");
30.
31. }
32.
33. - (void)applicationWillTerminate:(UIApplication *)application
34. {
35. NSLog(@"\n ===> 程序意外暫行 !");
36.
37. UIDevice *device = [UIDevice currentDevice];
38.
39. }

首次運行:

- (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

IOS applicationWillResignActive掛起