本篇文章主要介紹一些UIApplicationDelegate中幾個常用的回調(diào)方法的調(diào)用時機,以幫助你判斷哪些方法倒底放到哪個回調(diào)中去實現(xiàn)
1. – (void)applicationDidFinishLaunching:(UIApplication *)application;
此方法基本已經(jīng)棄用,改用第2個方法代替。
2. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
當應用程序啟動時(不包括已在后臺的情況下轉到前臺),調(diào)用此回調(diào)。launchOptions是啟動參數(shù),假如用戶通過點擊push通知啟動的應用,這個參數(shù)里會存儲一些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.
當應用程序全新啟動,或者在后臺轉到前臺,完全激活時,都會調(diào)用這個方法。如果應用程序是以前運行在后臺,這時可以選擇刷新用戶界面。
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.
當應用從活動狀態(tài)主動到非活動狀態(tài)的應用程序時會調(diào)用這個方法。這可導致產(chǎn)生某些類型的臨時中斷(如傳入電話呼叫或SMS消息)。或者當用戶退出應用程 序,它開始過渡到的背景狀態(tài)。使用此方法可以暫停正在進行的任務,禁用定時器,降低OpenGL ES的幀速率。游戲應該使用這種方法來暫停游戲。
調(diào)用時機可能有以下幾種:鎖屏,按HOME鍵,下接狀態(tài)欄,雙擊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
當用戶通過其它應用啟動本應用時,會回調(diào)這個方法,url參數(shù)是其它應用調(diào)用openURL:方法時傳過來的。
7. – (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
// try to clean up as much memory as possible. next step is to terminate app
當應用可用內(nèi)存不足時,會調(diào)用此方法,在這個方法中,應該盡量去清理可能釋放的內(nèi)存。如果實在不行,可能會被強行退出應用。
8. – (void)applicationWillTerminate:(UIApplication *)application;
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
當應用退出,并且進程即將結束時會調(diào)到這個方法,一般很少主動調(diào)到,更多是內(nèi)存不足時是被迫調(diào)到的,我們應該在這個方法里做一些數(shù)據(jù)存儲操作。
9.- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);
// one of these will be called after calling -registerForRemoteNotifications
當客戶端注冊遠程通知時,會回調(diào)上面兩個方法。
如果成功,則回調(diào)第一個,客戶端把deviceToken取出來發(fā)給服務端,push消息的時候要用。
如果失敗了,則回調(diào)第二個,可以從error參數(shù)中看一下失敗原因。
注:注冊遠程通知使用如下方法:
UIRemoteNotificationType t=UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:t];
10. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0);
當應用在前臺運行中,收到遠程通知時,會回調(diào)這個方法。
當應用在后臺狀態(tài)時,點擊push消息啟動應用,也會回調(diào)這個方法。
11. – (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
當應用收到本地通知時會調(diào)這個方法,同上面一個方法類似。
如果在前臺運行狀態(tài)直接調(diào)用,如果在后臺狀態(tài),點擊通知啟動時,也會回調(diào)這個方法
本地通知可見另一篇文章: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.
當用戶從臺前狀態(tài)轉入后臺時,調(diào)用此方法。使用此方法來釋放資源共享,保存用戶數(shù)據(jù),無效計時器,并儲存足夠的應用程序狀態(tài)信息的情況下被終止后,將應用 程序恢復到目前的狀態(tài)。如果您的應用程序支持后臺運行,這種方法被調(diào)用,否則調(diào)用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.
當應用在后臺狀態(tài),將要進行動前臺運行狀態(tài)時,會調(diào)用此方法。
如果應用不在后臺狀態(tài),而是直接啟動,則不會回調(diào)此方法。