AppDelegate中幾個(gè)常用的回調(diào)調(diào)用時(shí)機(jī)

本篇文章主要介紹一些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í)(不包括已在后臺(tái)的情況下轉(zhuǎn)到前臺(tái)),調(diào)用此回調(diào)。launchOptions是啟動(dòng)參數(shù),假如用戶通過點(diǎn)擊push通知啟動(dòng)的應(yīng)用,這個(gè)參數(shù)里會(huì)存儲(chǔ)一些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),或者在后臺(tái)轉(zhuǎn)到前臺(tái),完全激活時(shí),都會(huì)調(diào)用這個(gè)方法。如果應(yīng)用程序是以前運(yùn)行在后臺(tái),這時(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ù)存儲(chǔ)操作。

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)客戶端注冊(cè)遠(yuǎn)程通知時(shí),會(huì)回調(diào)上面兩個(gè)方法。

如果成功,則回調(diào)第一個(gè),客戶端把deviceToken取出來發(fā)給服務(wù)端,push消息的時(shí)候要用。

如果失敗了,則回調(diào)第二個(gè),可以從error參數(shù)中看一下失敗原因。

注:注冊(cè)遠(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)用在前臺(tái)運(yùn)行中,收到遠(yuǎn)程通知時(shí),會(huì)回調(diào)這個(gè)方法。

當(dāng)應(yīng)用在后臺(tái)狀態(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è)方法類似。

如果在前臺(tái)運(yùn)行狀態(tài)直接調(diào)用,如果在后臺(tái)狀態(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)前狀態(tài)轉(zhuǎn)入后臺(tái)時(shí),調(diào)用此方法。使用此方法來釋放資源共享,保存用戶數(shù)據(jù),無效計(jì)時(shí)器,并儲(chǔ)存足夠的應(yīng)用程序狀態(tài)信息的情況下被終止后,將應(yīng)用 程序恢復(fù)到目前的狀態(tài)。如果您的應(yīng)用程序支持后臺(tái)運(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)狀態(tài),將要進(jìn)行動(dòng)前臺(tái)運(yùn)行狀態(tài)時(shí),會(huì)調(diào)用此方法。

如果應(yīng)用不在后臺(tái)狀態(tài),而是直接啟動(dòng),則不會(huì)回調(diào)此方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容