本篇文章主要介紹一些UIApplicationDelegate中幾個(gè)常用的回調(diào)方法的調(diào)用時(shí)機(jī)。
以幫助你判斷哪些方法倒底放到哪個(gè)回調(diào)中去實(shí)現(xiàn)。
1. – (void)applicationDidFinishLaunching:(UIApplication *)application;
此方法基本已經(jīng)棄用,改用第2個(gè)方法代替备恤。
2. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
當(dāng)應(yīng)用程序啟動(dòng)時(shí)(不包括已在后臺的情況下轉(zhuǎn)到前臺)族沃,調(diào)用此回調(diào)误辑。launchOptions是啟動(dòng)參數(shù),假如用戶通過點(diǎn)擊push通知啟動(dòng)的應(yīng)用尤泽,這個(gè)參數(shù)里會(huì)存儲一些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.
當(dāng)應(yīng)用程序全新啟動(dòng),或者在后臺轉(zhuǎn)到前臺坯约,完全激活時(shí)熊咽,都會(huì)調(diào)用這個(gè)方法。如果應(yīng)用程序是以前運(yùn)行在后臺闹丐,這時(shí)可以選擇刷新用戶界面横殴。
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.
當(dāng)應(yīng)用從活動(dòng)狀態(tài)主動(dòng)到非活動(dòng)狀態(tài)的應(yīng)用程序時(shí)會(huì)調(diào)用這個(gè)方法。這可導(dǎo)致產(chǎn)生某些類型的臨時(shí)中斷(如傳入電話呼叫或SMS消息)卿拴∩缆兀或者當(dāng)用戶退出應(yīng)用程 序,它開始過渡到的背景狀態(tài)堕花。使用此方法可以暫停正在進(jìn)行的任務(wù)文狱,禁用定時(shí)器,降低OpenGL ES的幀速率缘挽。游戲應(yīng)該使用這種方法來暫停游戲瞄崇。
調(diào)用時(shí)機(jī)可能有以下幾種:鎖屏,按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:
這個(gè)方法已不再支持摹蘑,可能會(huì)在以后某個(gè)版本中去掉。建議用下面第6個(gè)方法代替
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
當(dāng)用戶通過其它應(yīng)用啟動(dòng)本應(yīng)用時(shí)轧飞,會(huì)回調(diào)這個(gè)方法纹蝴,url參數(shù)是其它應(yīng)用調(diào)用openURL:方法時(shí)傳過來的庄萎。
7. – (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
// try to clean up as much memory as possible. next step is to terminate app
當(dāng)應(yīng)用可用內(nèi)存不足時(shí),會(huì)調(diào)用此方法塘安,在這個(gè)方法中糠涛,應(yīng)該盡量去清理可能釋放的內(nèi)存。如果實(shí)在不行兼犯,可能會(huì)被強(qiáng)行退出應(yīng)用忍捡。
8. – (void)applicationWillTerminate:(UIApplication *)application;
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
當(dāng)應(yīng)用退出,并且進(jìn)程即將結(jié)束時(shí)會(huì)調(diào)到這個(gè)方法切黔,一般很少主動(dòng)調(diào)到砸脊,更多是內(nèi)存不足時(shí)是被迫調(diào)到的,我們應(yīng)該在這個(gè)方法里做一些數(shù)據(jù)存儲操作纬霞。
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);
當(dāng)客戶端注冊遠(yuǎn)程通知時(shí)凌埂,會(huì)回調(diào)上面兩個(gè)方法。
如果成功诗芜,則回調(diào)第一個(gè)瞳抓,客戶端把deviceToken取出來發(fā)給服務(wù)端,push消息的時(shí)候要用伏恐。
如果失敗了孩哑,則回調(diào)第二個(gè),可以從error參數(shù)中看一下失敗原因翠桦。
注:注冊遠(yuǎn)程通知使用如下方法:
UIRemoteNotificationType t=UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:t];
10. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0);
當(dāng)應(yīng)用在前臺運(yùn)行中横蜒,收到遠(yuǎn)程通知時(shí),會(huì)回調(diào)這個(gè)方法销凑。
當(dāng)應(yīng)用在后臺狀態(tài)時(shí)丛晌,點(diǎn)擊push消息啟動(dòng)應(yīng)用,也會(huì)回調(diào)這個(gè)方法斗幼。
11. – (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
當(dāng)應(yīng)用收到本地通知時(shí)會(huì)調(diào)這個(gè)方法茵乱,同上面一個(gè)方法類似。
如果在前臺運(yùn)行狀態(tài)直接調(diào)用孟岛,如果在后臺狀態(tài),點(diǎn)擊通知啟動(dòng)時(shí)督勺,也會(huì)回調(diào)這個(gè)方法
本地通知可見另一篇文章: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.
當(dāng)用戶從臺前狀態(tài)轉(zhuǎn)入后臺時(shí)渠羞,調(diào)用此方法。使用此方法來釋放資源共享智哀,保存用戶數(shù)據(jù)次询,無效計(jì)時(shí)器,并儲存足夠的應(yīng)用程序狀態(tài)信息的情況下被終止后瓷叫,將應(yīng)用 程序恢復(fù)到目前的狀態(tài)屯吊。如果您的應(yīng)用程序支持后臺運(yùn)行送巡,這種方法被調(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.
當(dāng)應(yīng)用在后臺狀態(tài)骗爆,將要進(jìn)行動(dòng)前臺運(yùn)行狀態(tài)時(shí),會(huì)調(diào)用此方法蔽介。
如果應(yīng)用不在后臺狀態(tài)摘投,而是直接啟動(dòng),則不會(huì)回調(diào)此方法虹蓄。