iOS 7 的新後台多任務(multitasking)機制是如何工作的?

我試了用QQ音樂下載然後退出切換到其它應用,再次切換回來後確實已經下載完了,還有打開了WSJ視頻應用,進入多任務切換時視頻沒有停止依然在播放。

iOS 7 learns when you like to use your apps and can update your content before you launch them. So if you tend to check your favorite social app at 9:00 a.m. every day, your feed will be ready and waiting for you. That』s multitasking in iOS 7. It knows what you want to do before you do. via Apple - iOS 7


你使用的程序不太可能是針對ios7優化的,so可以參考這幅圖(ios7前的一般程序多任務)

可以看到,應用其實有十分鐘後台的權利

除此之外還有兩種所謂的偽後台,一種是系統推送機制,另一種是墓碑式後台(在關閉程序時保存現場,下次啟動時還原,給人一種後台的假象)

=========================================================

ios7之後(一個app可以混合調用多種模式):

Background Audio,VoIP屬於不後台會死的類型;Location Services是地點服務,系統統一管理;Newsstand是蘋果自家應用,定時更新。這些都是早就有,沒變化。

Background Task Completion

ios4中開始加入的限時後台,允許應用連續後台十分鐘,ios7改為允許在開屏下運行十分鐘,關屏會暫停,直到開屏後重啟繼續湊足十分種。

Background Fetch

ios7新增,在系統認為合適的時候激活應用,具體什麼時候合適,開發者不可知,使用者不可控,由系統智能判斷,適合獲取及時性不強的信息

這個智能體現在系統會根據用戶啟動應用的頻率和時間以及當前的網路和電量情況來分配每個應用的獲取頻率和時間,數據刷新是統一的,即系統可以在一個進程內獲取多個應用所需的數據而不是一個應用一個進程(類似統一的推送機制,都是為了省電)

統一刷新這方面有點類似小米說的對齊喚醒和Deep Sleep Battery Saver,不同應用後台喚醒時刻不同會導致cpu無法進入deep sleep,某些Android用戶會發現自己的手機一夜掉電超多,其實就是某些糟糕的app讓cpu沒睡好:-)

Remote Notification

ios7新增,由統一推送通知激活後台,支持silent notification,適合實時類強的應用,有頻率限制(每小時幾次)

Background Transfer Service

ios7新增,可以由應用直接激活的無限制後台,許可權大,但是只限於下載上傳類服務,只會在 WiFi 環境下才進行傳輸

需要特別注意的是:應用被Background Fetch、Remote Notification、Background Transfer Service三類喚醒時,只被給予幾秒鐘時間來處理更新。

=========================================================

可以看到對於Remote Notification和Background Transfer Service還是有一定風險的。。。蘋果說會記錄每次操作的時間,從而對應用進行監管,具體怎樣,可觀後效~

以上整合各種資料,非專業級回答,歡迎指正


現有第三方程序更新之前,無法使用iOS7新的多任務機制。

iOS 7 supports the following new background execution modes for apps:

  • Apps that regularly require new content can register with the system and be woken up or launched periodically to download that content in the background. To register, include the UIBackgroundModes key with the fetch value in your app』s Info.plist file and set the minimum time you want between fetch operations using the setMinimumBackgroundFetchInterval: method. You must also implement theapplication:performFetchWithCompletionHandler: method in your app delegate to perform any downloads.

  • Apps that use push notifications to notify the user that new content is available can now use those notifications to initiate background download operations. To support this mode, include the UIBackgroundModes key with the remote-notification value in your app』s Info.plist file. Your app delegate must also implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method.

Apps supporting either the fetch or remote-notification background modes may be launched or moved from the suspended to background state at appropriate times. In the case of the fetch background mode, the system uses available information to determine the best time to launch or wake apps. For example, it does so when networking condition are good or when the device is already awake. Apps supporting the remote-notification background mode may be woken up when a new push notification arrives but before that notification is delivered to the user. The app can use the interval to download new content and have that content ready to present to the user when the notification is subsequently delivered.

To handle the downloading of content in the background, apps should use the new NSURLSession class. This class improves on the existingNSURLConnection class by providing a simple, task-based interface for initiating and processing NSURLRequest objects. A single NSURLSession object can initiate multiple download and upload tasks, and through its delegate can handle any authentication requests coming from the server.


推薦閱讀:

Apple id 賬戶的密碼是否明文儲存?
為什麼iOS的beta系統總是很卡漏洞很多?
蘋果手機為什麼對微軟公司員工打折?
蘋果這次iPad Air為什麼沒搞飢餓營銷?
iPad Pro 作為上課記錄筆記的工具怎麼樣?

TAG:iPhone | iOS | 蘋果公司AppleInc | iOS7 |