WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge
WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge
我终于(忽略我从未见过的示例代码 "application task received, start URL session")设法让我的 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:
委托方法,该方法有错误:
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be ... which could put your confidential information at risk."
有没有人得到后台监视更新来满足在后台 URL 会话期间点击 didReceiveChallenge
方法的额外要求?
感谢您提供的任何帮助或建议。
事实证明,服务器证书错误实际上是由于我们测试环境中的一种罕见情况造成的。在后端人员为我们解决该问题后,这段代码在我们的生产和测试环境中都运行良好。
我从来没有打过 urlSession:task:didReceiveChallenge:
但事实证明我不需要。
做了一个不相关的小改动:
如果没有 prints/breakpoints,我有时会在点击 downloadTask:didFinishDownloadingTo location:
之前像毫秒一样点击 task:didCompleteWithError Error:
。
所以我改为设置 self.pendingBackgroundURLTask 在 downloadTask:didFinishDownloadingTo location:
中完成。我只设置它在 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
}
希望对其他人有所帮助。
我终于(忽略我从未见过的示例代码 "application task received, start URL session")设法让我的 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:
委托方法,该方法有错误:
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be ... which could put your confidential information at risk."
有没有人得到后台监视更新来满足在后台 URL 会话期间点击 didReceiveChallenge
方法的额外要求?
感谢您提供的任何帮助或建议。
事实证明,服务器证书错误实际上是由于我们测试环境中的一种罕见情况造成的。在后端人员为我们解决该问题后,这段代码在我们的生产和测试环境中都运行良好。
我从来没有打过 urlSession:task:didReceiveChallenge:
但事实证明我不需要。
做了一个不相关的小改动:
如果没有 prints/breakpoints,我有时会在点击 downloadTask:didFinishDownloadingTo location:
之前像毫秒一样点击 task:didCompleteWithError Error:
。
所以我改为设置 self.pendingBackgroundURLTask 在 downloadTask:didFinishDownloadingTo location:
中完成。我只设置它在 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
}
希望对其他人有所帮助。