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

后端 未结 2 1488
醉酒成梦
醉酒成梦 2020-12-16 03:36

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

相关标签:
2条回答
  • 2020-12-16 03:45

    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.

    0 讨论(0)
  • 2020-12-16 03:48

    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)

    0 讨论(0)
提交回复
热议问题