iOS:在应用程序处于后台时执行上载任务

Chr*_*fer 13 upload background ios

当iOS应用程序在后台时,真的没有办法运行UPLOAD任务吗?这是荒唐的.一直在看各种各样的东西NSURLSessionUploadTask,dispatch_after甚至NSTimer,但没有任何东西比应用程序在后台放置后的10秒钟更有效.

其他具有上传功能的应用如何运作?比如说,将图像上传到Facebook并将应用程序置于后台会取消上传吗?

为什么iOS不能拥有Android和Windows Phone等后台服务或代理?

这是我的应用程序的一个关键功能,在其他平台上运行完美.

任何帮助表示赞赏:(

Rob*_*Rob 18

如果background(withIdentifier:)在实例化时使用配置,则可以使用iOS 7+继续在后台上传URLSession.

注意:


顺便说一句,如果您不想使用背景NSURLSession,但是想要在应用程序离开背景后继续运行有限长度的任务超过几秒钟,您可以请求更多时间使用UIApplication方法beginBackgroundTaskWithExpirationHandler.这将为您提供几分钟的时间来完成您正在处理的任何任务.请参阅清单1中的扩展应用程序的后台执行时间:

func sendDataToServer( data : NSData ) {
   // Perform the task on a background queue.
   DispatchQueue.global().async {
      // Request the task assertion and save the ID.
      self.backgroundTaskID = UIApplication.shared.
                 beginBackgroundTask (withName: "Finish Network Tasks") {
         // End the task if time expires.
         UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
         self.backgroundTaskID = UIBackgroundTaskInvalid
      }

      // Send the data synchronously.
      self.sendAppDataToServer( data: data)

      // End the task assertion.
      UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
      self.backgroundTaskID = UIBackgroundTaskInvalid
   }
}
Run Code Online (Sandbox Code Playgroud)