AppDelegate中幾個常用的回調調用時機

來源:互聯網
上載者:User

標籤:

本篇文章主要介紹一些UIApplicationDelegate中幾個常用的回調方法的調用時機。
以協助你判斷哪些方法倒底放到哪個回調中去實現。

1. – (void)applicationDidFinishLaunching:(UIApplication *)application;
此方法基本已經棄用,改用第2個方法代替。
2. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
當應用程式啟動時(不包括已在背景情況下轉到前台),調用此回調。launchOptions是啟動參數,假如使用者通過點擊push通知啟動的應用,這個參數裡會儲存一些push通知的資訊。

3. – (void)applicationDidBecomeActive:(UIApplication *)application;
// 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.
當應用程式全新啟動,或者在後台轉到前台,完全啟用時,都會調用這個方法。如果應用程式是以前運行在後台,這時可以選擇重新整理使用者介面。

4. – (void)applicationWillResignActive:(UIApplication *)application;
// 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.
當應用從活動狀態主動到非使用中的應用程式時會調用這個方法。這可導致產生某些類型的臨時中斷(如傳入撥打電話或SMS訊息)。或者當使用者退出應用程 序,它開始過渡到的背景狀態。使用此方法可以暫停進行中的任務,禁用定時器,降低OpenGL ES的畫面播放速率。遊戲應該使用這種方法來暫停遊戲。
調用時機可能有以下幾種:鎖屏,按HOME鍵,下接狀態列,雙擊HOME鍵彈出低欄,等情況。

5. – (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;
// Will be deprecated at some point, please replace with application:openURL:sourceApplication:annotation:
這個方法已不再支援,可能會在以後某個版本中去掉。建議用下面第6個方法代替

6. – (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation NS_AVAILABLE_IOS(4_2);
// no equiv. notification. return NO if the application can’t open for some reason
當使用者通過其它應用啟動本應用時,會回調這個方法,url參數是其它應用調用openURL:方法時傳過來的。

7. – (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
// try to clean up as much memory as possible. next step is to terminate app
當應用可用記憶體不足時,會調用此方法,在這個方法中,應該盡量去清理可能釋放的記憶體。如果實在不行,可能會被強行退出應用。

8. – (void)applicationWillTerminate:(UIApplication *)application;
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
當應用退出,並且進程即將結束時會調到這個方法,一般很少主動調到,更多是記憶體不足時是被迫調到的,我們應該在這個方法裡做一些資料存放區操作。

9. // one of these will be called after calling -registerForRemoteNotifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0);
當用戶端註冊遠程通知時,會回調上面兩個方法。
如果成功,則回調第一個,用戶端把deviceToken取出來發給服務端,push訊息的時候要用。
如果失敗了,則回調第二個,可以從error參數中看一下失敗原因。
註:註冊遠程通知使用如下方法:

UIRemoteNotificationType t=UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;[[UIApplication sharedApplication] registerForRemoteNotificationTypes:t];

10. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0);
當應用在前台運行中,收到遠程通知時,會回調這個方法。
當應用在後台狀態時,點擊push訊息啟動應用,也會回調這個方法。
11. – (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
當應用收到本地通知時會調這個方法,同上面一個方法類似。
如果在前台運行狀態直接調用,如果在後台狀態,點擊通知啟動時,也會回調這個方法
本地通知可見另一篇文章:http://bluevt.org/?p=70

12. – (void)applicationDidEnterBackground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
// 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.
當使用者從台前狀態轉入後台時,調用此方法。使用此方法來釋放資源共用,儲存使用者資料,無效計時器,並儲存足夠的應用程式狀態資訊的情況下被終止後,將應用 程式恢複到目前的狀態。如果您的應用程式支援後台運行,這種方法被調用,否則調用applicationWillTerminate:使用者退出。

13. – (void)applicationWillEnterForeground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
// 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.
當應用在後台狀態,將要進行動前台運行狀態時,會調用此方法。
如果應用不在後台狀態,而是直接啟動,則不會回調此方法。

AppDelegate中幾個常用的回調調用時機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.