iOS 后台获取

iOS background fetch

我对后台提取有点困惑。我在 Apple Developer 文档中读到,当 OS 决定它应该执行时,用户无法控制后台获取,而在 Apple Developer 论坛 post 上,Apple 员工说如果用户终止应用程序(双击在主页和按钮上滑动)后台获取不会发生,在这种情况下用户可以控制后台获取。如果用户使用任务管理器杀死应用程序,那么有人可以向我澄清一下,后台获取仍然会在后台继续,或者它与应用程序同时被杀死。

Apple 文档:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1

来自您的文档 link:

When a good opportunity arises, the system wakes or launches your app into the background and calls the app delegate’s application:performFetchWithCompletionHandler: method.

因此,系统似乎能够在后台启动非 运行 的应用程序,因此它会执行后台提取。不过,另一方面,您可以在文档的后面阅读:

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.

因此,Apple 的工程师是对的:强制退出应用程序会使它进入一种特殊情况,不再允许后台提取。

我认为这句话(来自链接文档)对于您所描述的场景最重要:

Once configured, your NSURLSession object seamlessly hands off upload and download tasks to the system at appropriate times. If tasks finish while your app is still running (either in the foreground or the background), the session object notifies its delegate in the usual way. If tasks have not yet finished and the system terminates your app, the system automatically continues managing the tasks in the background. If the user terminates your app, the system cancels any pending tasks.

如果用户觉得有必要允许后台操作,他就不会杀死应用程序。但是当他杀死它时,只有禁止后台获取才是合适的。用户只能通过允许它留在 background/by 杀死应用程序来控制是否应该进行后台获取。但是一旦应用程序处于后台,用户就无法控制 "when" 后台获取的发生。 OS 根据它的空闲程度来确定它。