Background Fetch
Fetching Small Amounts of Content Opportunistically
Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background fetch option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the fetch value in your app’s Info.plist file.) Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so.
When a good opportunity arises, the system wakes or launches your app into the background and calls the app delegate’s application:performFetchWithCompletionHandler: method. Use that method to check for new content and initiate a download operation if content is available. As soon as you finish downloading the new content, you must execute the provided completion handler block, passing a result that indicates whether content was available. Executing this block tells the system that it can move your app back to the suspended state and evaluate its power usage. Apps that download small amounts of content quickly, and accurately reflect when they had content available to download, are more likely to receive execution time in the future than apps that take a long time to download their content or that claim content was available but then do not download anything.
When downloading any content, it is recommended that you use the NSURLSession class to initiate and manage your downloads. For information about how to use this class to manage upload and download tasks, see URL Loading System Programming Guide.
在合适的时机更新数据
需要定期更新内容的App可以要求系统在合适的时机唤醒它们,以便更新数据。要支持此功能,需要在Xcode项目的Capabilities
选项卡的Background modes
中启用Background fetch
选项(也可以通过在应用程序的info.plist
文件中对UIBackgroundModes
字段中添加fetch
)。启用此功能并不能保证系统会在任何时间给唤起App并执行后台数据获取。系统会平衡应用各个App后台获取内容的需求之后,会给出"合适时机"唤起对应App并执行后台数据获取。
当"合适时机"出现时,系统将后台唤起App,并调用App的delegate回调application:performFetchWithCompletionHandler:。在该回调中检查新内容,并启动下载操作。下载完新内容后,必须执行提供的completionHandler
,并传递FetchResult
结果,指示这次更新的内容是否可用。completionHandler
会通知系统可以将App切换至挂起状态并评估其电源使用情况。快速的完成内容下载并准确反映内容的可用性的App,会比内容下载耗时长或声明需要内容但不下载的App,在未来更有可能收到后台唤起的机会。
下载任何内容时,建议使用NSURLSession类管理下载。有关信息,请参阅URL Loading System Programming Guide
。
Understanding When Your App Gets Launched into the Background
Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:
- For location apps:
- The system receives a location update that meets the app’s configured criteria for delivery.
- The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)
- For audio apps
- The audio framework needs the app to process some data. (Audio apps include those that play audio or use the microphone.)
- For Bluetooth apps:
- An app acting in the central role receives data from a connected peripheral.
- An app acting in the peripheral role receives commands from a connected central.
- For background download apps:
- A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1.
- The system wakes the app at opportunistic moments to begin downloading new content.
- For apps downloading content in the background using the NSURLSession class, all tasks associated with that session object either completed successfully or received an error.
- A download initiated by a Newsstand app finishes.
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system. When password protection is enabled on the device, the system does not launch an app in the background before the user first unlocks the device.
后台启动App
App支持后台启动并处理一些事件,对于用户主动Kill App,系统只会在以下事件发生时才会后台启动App:
- 定位相关App
- 系统接收到符合App配置信息的位置更新
- 设备进入或退出已指定区域(区域可以是地理区域或iBeacon区域)
- 音频相关App
- audio framework需要应用程序处理一些数据(音频应用程序包括播放音频或使用麦克风)
- 蓝牙相关App
- App通过蓝牙发送数据
- App通过蓝牙接收数据
- 支持后台更新App
- aps消息体包含了
content-available: 1
的键值对的静默推送 - 系统会在“合适时机”唤醒App,开始下载新内容
- 使用
NSURLSession
在后台下载内容,并向系统反馈成功与失败 - 由
Newsstand App
触发的数据下载
- aps消息体包含了
在大多数情况下,用户强制退出应用程序后,系统不会重新启动应用程序。一个例外是定位Apps,在iOS 8和更高版本中,它在用户强制退出后会重新启动。但在特定情况下,系统可以自动在后台启动,前提是用户主动启动过应用程序或重新启动设备。当设备上启用密码保护时,在用户首次解锁设备之前,系统不会在后台启动应用程序。
总结
Background Fetch 具有下面几个特性:
- 系统调度,对电量和数据的使用敏感
- 后台启动与正常启动表现一致
- 与应用实际的运行状态无关,可唤醒(进程从挂起到恢复)或启动(进程从无到有)
- 系统必须在30秒内调用
completionHandler
,否则进程将被杀死(performFetchWithCompletionHandler
到completionHandler
)