WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge

我终于(忽略了我从来没有看到工作过去的示例代码,“接收到应用程序任务,启动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:task:didReceiveChallenge: 从来没有被打 ,所以我不能让我的下载完成。 (我也添加了会话级别的urlSession:didReceiveChallenge:委托方法,它也没有被击中)。

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

“这个服务器的证书是无效的,你可能正在连接到一个服务器,这个服务器可能会把你的机密信息置于危险之中。”

有没有人得到了后台手表更新来处理在后台URL会话期间碰到didReceiveChallenge方法的附加要求?

你可以提供任何帮助或build议表示赞赏。

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

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

做了一个小的不相关的变化:
没有打印/断点,我有时打task:didCompleteWithError Error:像一个毫秒之前,我击中downloadTask:didFinishDownloadingTo location:

所以我反而设置self.pendingBackgroundURLTask完成downloadTask:didFinishDownloadingTo location: 。 我只在task:didCompleteWithError Error:设置完成task:didCompleteWithError Error: if 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 } 

希望别人认为这有帮助。