WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge [英] WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge

查看:138
本文介绍了WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have finally (ignoring the sample code which I never saw work past "application task received, start URL session") managed to get my WatchOS3 code to start a background URL Session task as follows:

 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:委托方法,它也没有被点击).

However, the issue I am now seeing is that my delegate method urlSession:task:didReceiveChallenge: is never being hit, so I cannot get my download to complete. (I have also added the session level urlSession:didReceiveChallenge: delegate method and it is also not being hit).

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

Instead I immediately hit my task:didCompleteWithError: delegate method which has the error:

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

"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方法的额外要求吗?

Has anyone gotten the background watch update to work with the additional requirement of hitting the didReceiveChallenge method during the background URL session?

感谢您提供的任何帮助或建议.

Any help or advice you can offer is appreciated.

推荐答案

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

As it turns out the server certificate error was actually due to a rare scenario in our test environments. After the back end folks gave us a work around for that issue this code worked fine in both our production and test environments.

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

I never hit urlSession:task:didReceiveChallenge: but it turned out I did not need to.

进行了不相关的小更改:
没有打印点/断点,我有时会在按下downloadTask:didFinishDownloadingTo location:之前像ms一样按下task:didCompleteWithError Error:.

Made a minor un-related change:
Without prints/breakpoints I was sometimes hitting task:didCompleteWithError Error: like a ms before I hit downloadTask:didFinishDownloadingTo location:.

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

So I instead set self.pendingBackgroundURLTask completed in downloadTask:didFinishDownloadingTo location:. I only set it completed in 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
}

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

Hope someone else finds this helpful.

这篇关于WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆