iOS開發(fā)的過程中常常會有按下home鍵盤,進入后臺,但不希望當前任務立即停止的情況,比如保存數(shù)據(jù),斷開鏈接,繼續(xù)下載文件等,接下來就簡單聊下iOS的后臺任務。
一,后臺任務的分類
程序的5個狀態(tài)和對應的AppDelegate的7個方法 :
- Not Running, 未運行
- Inactive, 非活動
- Active, 活動
- Background, 后臺
- Suspend, 掛起
對應的方法分別是:
// 進程啟動但還沒完成初始化,這個方法是iOS6之后才有的
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// 進程啟動基本完成
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// 應用程序將要入非活動狀態(tài)執(zhí)行,在此期間,應用程序不接收消息或事件
- (void)applicationWillResignActive:(UIApplication *)application
// 應用程序入活動狀態(tài),這個剛好跟上面那個方法相反
- (void)applicationDidBecomeActive:(UIApplication *)application
// 程序被推送到后臺,如果要設置后臺繼續(xù)運行,則在這個函數(shù)里面設置即可
- (void)applicationDidEnterBackground:(UIApplication *)application
// 程序從后臺將要回到前臺
- (void)applicationWillEnterForeground:(UIApplication *)application
// 程序將要退出
- (void)applicationWillTerminate:(UIApplication *)application
在介紹iOS應用狀態(tài)5種最基本的狀態(tài)時,我們發(fā)現(xiàn)前臺運行有兩種狀態(tài),分別是Inactive和Active狀態(tài)。大多數(shù)情況下,Inactive狀態(tài)只是其它狀態(tài)之間切換時短暫的停留狀態(tài),如前后臺應用切換時,Inactive狀態(tài)會在Active和Background之間短暫出現(xiàn),比如App Switcher/回到原應用的操作等。
用戶在按下home鍵后,app可做的事情有很多,比如聽歌、打電話、下載電影、更新數(shù)據(jù)、定時任務等,可以大致分為兩類,注冊任務(耗時長)和非注冊任務(耗時較短)。
二,非注冊任務的運行
非注冊任務一般耗時較短,多用來保存數(shù)據(jù)或延遲執(zhí)行某一命令等,通過系統(tǒng)API即可實現(xiàn)。可以先看一段官方實例代碼:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
}
beginBackgroundTaskWithName:expirationHandler:
方法標識了一個后臺任務的開始,并用過超時處理的回調來結束此任務。那么,超時時間具體是多少?可以通過UIApplication的只讀屬性backgroundTimeRemaining來獲取當前后臺任務執(zhí)行的剩余時間,它不是具體的數(shù)字(我執(zhí)行的時間大概160秒),而是iOS根據(jù)當前系統(tǒng)環(huán)境綜合考量后估算出來的。然后執(zhí)行expirationHandler回調完成一個后臺任務的執(zhí)行周期。
三,注冊任務的流程
有些耗時較長的工作,則需要申請專門的權限來保證正常執(zhí)行而不被掛起,只有少數(shù)幾種類型被允許這個做。
- Apps that play audible content to the user while in the background, such as a music player app
- Apps that record audio content while in the background
- Apps that keep users informed of their location at all times, such as a navigation app
- Apps that support Voice over Internet Protocol (VoIP)
- Apps that need to download and process new content regularly
- Apps that receive regular updates from external accessories
申請使用以上場景的后臺權限需要在Xcode->Capabilities->Background Mode中配置,如下圖所示:
勾選所需的模式后,會自動在app的info.plist文件中添加Required background modes一項,包含了所勾選的后臺運行模式。如下所示:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>voip</string>
</array>
以Background Fetch為例,具體說下任務流程。
首先需要在Xcode Capabilities 中開啟Background fetch選項,在didFinishLaunchingWithOptions
中設置下獲取的時間間隔:
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
如果不對最小后臺獲取間隔進行設定的話,系統(tǒng)將使用默值UIApplicationBackgroundFetchIntervalNever
,也就是永遠不進行后臺獲取。而最小的時間間隔則有系統(tǒng)根據(jù)電量、網(wǎng)絡狀態(tài)、用戶使用習慣等綜合考量后來設定一定的差值,執(zhí)行fetch任務,具體代碼如下:
//File: YourAppDelegate.m
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController;
id fetchViewController = navigationController.topViewController;
if ([fetchViewController respondsToSelector:@selector(fetchDataResult:)]) {
[fetchViewController fetchDataResult:^(NSError *error, NSArray *results){
if (!error) {
if (results.count != 0) {
//Update UI with results.
//Tell system all done.
completionHandler(UIBackgroundFetchResultNewData);
} else {
completionHandler(UIBackgroundFetchResultNoData);
}
} else {
completionHandler(UIBackgroundFetchResultFailed);
}
}];
} else {
completionHandler(UIBackgroundFetchResultFailed);
}
}
與fetch類型的是,后臺任務同樣可以滿足遠程通知喚醒app執(zhí)行某一進程,結束后以本地通知的方式提醒用戶,以靜默、智能的方式帶給用戶使用上的絕佳體驗,相似的場景還有后臺電影、音樂的下載等,在此不多做介紹。
四,后臺任務的注意事項
1,關于OpenGL ES
有些基于位置的app需要后臺定時更新用戶的當前位置,導致未知崩潰。原因就是Location update類型的后臺任務在更新位置時,需要重新繪制MKMapView,調用了OpenGL ES,而OpenGL ES必須在程序Inactive以前關閉,不然會crash。如官方文檔描述:To summarize, your app needs to call the glFinish
function to ensure that all previously submitted commands are drained from the command buffer and are executed by OpenGL ES. After it moves into the background, you must avoid all use of OpenGL ES until it moves back into the foreground.
可以在更新位置之前做一下app狀態(tài)的檢測:
if( (appState != UIApplicationStateBackground) && (appState != UIApplicationStateInactive)) {
// update location
}
2,關于CAAnimation動畫
OpenGL提供了Core Animation的基礎,它是底層的C接口,直接和iPhone,iPad的硬件通信,極少地抽象出來的方法。當app進入background模式時,所有基于Core Animation 的動畫將自動停止,這是因為動畫的渲染需要調用Open GL或者Core Graphics來實現(xiàn)UIKit層的變動,然后在applicationWillEnterForeground的時候重新啟動即可。
參考: