WKURLSessionRefreshBackgroundTask isn't called when attempting to do background refreshes in watchOS

旧巷老猫 提交于 2019-11-30 09:36:55
CodenameDuchess

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:

  1. Make the MainInterfaceController an URLSessionDelegate
  2. Create a local variable: var savedTask:WKRefreshBackgroundTask?
  3. In the handle( backgroundTasks:) function, save the WKURLSessionRefreshBackgroundTaskto the local variable self.savedTask = task - do not task.setTaskCompleted() (!!)
  4. In the urlSession - didFinishDownloading:, set the saved task to completed: self.savedTask?.setTaskCompleted() after you've read the data received.
  5. Ensure that the resource you are accessing is https://, otherwise add the "App Transport Security Settings" to your Info.plist file.

Hope this can help!

ps. this works in the simnulator (xcode 8.3.3, watchOS 3.2)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!