I\'m working on a complication to get scheduled data from a web service. Every 20-30 minutes (or manually), I am scheduling a WKRefreshBackgroundTask to do this.
As
I never did get Apple's example working, but I did get background refresh working. Unlike their example you will need to make yourself the delegate of the session. So create your background session like this instead:
let backgroundSession = URLSession(configuration: backgroundSessionConfig, delegate: self, delegateQueue: nil)
I have a detailed code example here (see the question's code):
WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge
Hope it helps.
I got it working! I started out by looking a lot at the WWDC'16 videoes/notes, comparing them to the WatchBackgroundRefresh example from Apple. Then, while browsing the Apple Dev forums, I found this thread, with the same problem we encountered. There, a guy, "Daniel Fontes" posts his 'recipe' for getting it to work (note, not the accepted answer!), and that combined with the videos worked for me.
What I did to make the Apple example work:
var savedTask:WKRefreshBackgroundTask?
handle( backgroundTasks:)
function, save the WKURLSessionRefreshBackgroundTask
to the local variable self.savedTask = task
- do not task.setTaskCompleted() (!!) urlSession - didFinishDownloading:
, set the saved task to completed: self.savedTask?.setTaskCompleted()
after you've read the data received.Hope this can help!
ps. this works in the simnulator (xcode 8.3.3, watchOS 3.2)