我终于(忽略了在“收到应用程序任务,启动 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 会话期间点击该方法的额外要求?
感谢您提供任何帮助或建议。