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

 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:,永远​​不会被命中,所以我无法完成下载。 (我还添加了 session 级别urlSession:didReceiveChallenge:委托方法,它也没有被点击)。

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

“此服务器的证书无效。您可能正在连接到冒充……的服务器,这可能会使您的机密信息受到威胁。”

是否有人在后台URL session 期间更新了后台监视,以符合点击didReceiveChallenge方法的额外要求?

您可以提供的任何帮助或建议都将受到赞赏。

最佳答案

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

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

进行了一些无关的小更改:
没有打印/断点,有时我会像ms一样先击task:didCompleteWithError Error:,然后再击downloadTask:didFinishDownloadingTo location:

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

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
}

希望其他人对此有所帮助。

关于ios - WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40404749/

10-08 20:51