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.
source share