1

我终于(忽略了在“收到应用程序任务,启动 URL 会话”之后从未见过的示例代码)设法让我的 WatchOS3 代码启动后台 URL 会话任务,如下所示:

 func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {

    for task in backgroundTasks {
        if let refreshTask = task as? WKApplicationRefreshBackgroundTask {
            // this task is completed below, our app will then suspend while the download session runs
            print("application task received, start URL session")

            let request = self.getRequestForRefresh()
            let backgroundConfig = URLSessionConfiguration.background(withIdentifier: NSUUID().uuidString)
            backgroundConfig.sessionSendsLaunchEvents = true
            backgroundConfig.httpAdditionalHeaders = ["Accept":"application/json"]
            let urlSession = URLSession(configuration: backgroundConfig, delegate: self, delegateQueue: nil)
            let downloadTask = urlSession.downloadTask(with: request)

            print("Dispatching data task at \(self.getTimestamp())")
            downloadTask.resume()

            self.scheduleNextBackgroundRefresh(refreshDate: self.getNextPreferredRefreshDate())
            refreshTask.setTaskCompleted()
        }
        else if let urlTask = task as? WKURLSessionRefreshBackgroundTask {
            //awakened because background url task has completed
            let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: urlTask.sessionIdentifier)
            self.backgroundUrlSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil) //set to nil in task:didCompleteWithError: delegate method

            print("Rejoining session ", self.backgroundUrlSession as Any)
            self.pendingBackgroundURLTask = urlTask //Saved for .setTaskComplete() in downloadTask:didFinishDownloadingTo location: (or if error non nil in task:didCompleteWithError:) 

        } else {
            //else different task, not handling but must Complete all tasks (snapshot tasks hit this logic)
            task.setTaskCompleted()
        }
    }
}

但是,我现在看到的问题是我的委托方法 urlSession:task:didReceiveChallenge:永远不会被击中,所以我无法完成下载。(我还添加了会话级别的 urlSession:didReceiveChallenge: 委托方法,它也没有被击中)。

相反,我立即点击了task:didCompleteWithError:有错误的委托方法:

“此服务器的证书无效。您可能正在连接到假装的服务器......这可能会使您的机密信息面临风险。”

有没有人得到后台监视更新来处理didReceiveChallenge在后台 URL 会话期间点击该方法的额外要求?

感谢您提供任何帮助或建议。

4

1 回答 1

3

事实证明,服务器证书错误实际上是由于我们的测试环境中的罕见情况造成的。在后端人员为我们解决该问题后,该代码在我们的生产和测试环境中都运行良好。

我从来没有打过urlSession:task:didReceiveChallenge:,但事实证明我不需要。

做了一个不相关的小改动:
在没有打印/断点task:didCompleteWithError Error:的情况下,我有时会在点击downloadTask:didFinishDownloadingTo location:.

所以我改为将 self.pendingBackgroundURLTask 设置为完成downloadTask:didFinishDownloadingTo location:task:didCompleteWithError Error:如果错误!= nil ,我只将其设置为完成。

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

    //Complete task only if error, if no error it will be completed when download completes (avoiding race condition)
    if error != nil {
        self.completePendingBackgroundTask()
    }

}

func completePendingBackgroundTask()
{
    //Release the session
    self.backgroundUrlSession = nil

    //Complete the task
    self.pendingBackgroundURLTask?.setTaskCompleted()
    self.pendingBackgroundURLTask = nil
}

希望其他人觉得这很有帮助。

于 2016-11-14T17:59:15.877 回答