WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge

时间:2016-11-03 14:56:54

标签: ios swift watch-os-3 wkrefreshbackgroundtask

我终于(忽略了我从未见过工作的示例代码"收到应用程序任务,启动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:委托方法,该方法有错误:

&#34;此服务器的证书无效。您可能正在连接到假冒的服务器......这会使您的机密信息面临风险。&#34;

是否有人获得了后台监视更新,以便在后台网址会话期间使用didReceiveChallenge方法的额外要求?

您可以提供的任何帮助或建议表示赞赏。

1 个答案:

答案 0 :(得分:2)

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

我从来没有点过.... <link href="https://fonts.googleapis.com/css?family=Lato" <div class="rs"> This is text This is text This is text T </div> ,但事实证明我不需要。

做了一个小小的无关联的改变:
如果没有打印/断点,我有时会在urlSession:task:didReceiveChallenge:之前点击task:didCompleteWithError Error:

所以我改为在downloadTask:didFinishDownloadingTo location:中设置self.pendingBackgroundURLTask。我只在downloadTask:didFinishDownloadingTo location:中设置完成,如果错误!= nil。

task:didCompleteWithError Error:

希望别人觉得这很有帮助。