gpt4 book ai didi

ios - WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:33 25 4
gpt4 key购买 nike

我终于(忽略我从未见过在“收到应用程序任务,启动 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()
}
}
}

但是,我现在看到的问题是我的委托(delegate)方法urlSession:task:didReceiveChallenge: 从未被攻击,所以我无法完成下载。 (我还添加了 session 级 urlSession:didReceiveChallenge: 委托(delegate)方法,它也没有被命中)。

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

“此服务器的证书无效。您可能正在连接到伪装成……的服务器,这可能会使您的 secret 信息面临风险。”

有没有人得到后台监视更新来满足在后台 URL session 期间点击 didReceiveChallenge 方法的额外要求?

如果您能提供任何帮助或建议,我们将不胜感激。

最佳答案

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

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

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

所以我改为在 downloadTask:didFinishDownloadingTo location: 中设置 self.pendingBackgroundURLTask completed。我只将它设置为在 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
}

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com