URLSessionTask始终运行为错误

时间:2016-09-08 12:03:22

标签: xcode swift3 watch-os-3

在我的ExtensionDelegate中,我使用以下代码启动了一个URLSessionTask:

func scheduleNextURLSessionTask() {
    let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: "myIdentifier")

    let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil)
    let retrieveTask = backgroundSession.dataTask(with: URL(string: "https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple")!)
    retrieveTask.resume()
}

我的ExtensionDelegate当然实现了URLSessionDataDelegate。 这总是运行错误。手段

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

将触发将跟随错误消息:

  

URLSessionTask didCompleteWithError可选(错误域= NSURLErrorDomain代码= -997"丢失与后台传输服务的连接" UserInfo = {NSErrorFailingURLKey = https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple,NSErrorFailingURLStringKey = https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple,NSLocalizedDescription = Lost连接到后台传输服务})

任何想法在这里可能出错? scheduleNextURLSessionTask是从后台进程调用的(WKApplicationRefreshBackgroundTask)。

1 个答案:

答案 0 :(得分:0)

根据Apple文档,只能安排下载和上传任务作为后台任务执行:

“仅支持上传和下载任务(无数据任务)。”

在“后台转移注意事项” -

下查看here