WatchOS 3 WKApplicationRefreshBackgroundTask doneReceiveChallenge

Finally, I (ignoring the code sample that I never saw behind the “received application, start url session”), was able to get my WatchOS3 code to run the 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() } } } 

However, the problem that I am seeing now is that my delegation method urlSession:task:didReceiveChallenge: never hits , so I cannot complete the download. (I also added the urlSession level of the level: didReceiveChallenge: the delegation method, and it also doesn't get there).

Instead, I immediately hit my task:didCompleteWithError: delegation method, which has an error:

"The certificate for this server is not valid. Perhaps you are connecting to a server that is pretending ... that could threaten your sensitive information."

Has anyone received a background preview update to work with the additional didReceiveChallenge during a background URL session?

Any help or advice you can offer is appreciated.

0
source share
1 answer

As it turned out, the server certificate error was actually caused by a rare scenario in our test environments. After the people with feedback gave us work on this problem, this code worked perfectly in both our production and test environments.

I never hit urlSession:task:didReceiveChallenge: but it turned out that I didn't need to.

A minor unrelated change has been made:
Without fingerprints / breakpoints, I sometimes clicked on task:didCompleteWithError Error: like ms before I hit downloadTask:didFinishDownloadingTo location:

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

0
source

Source: https://habr.com/ru/post/1013171/


All Articles