Looking at background fetch in more depth

Now that you have a broad understanding of background fetch; let's investigate what your app needs to do to be compatible with background fetch. First of all, you should let iOS know that your app wants to be woken up periodically. To do this, you will need to turn on the Background Modes capability for your app. Within this capability, you can opt in to background fetch. Enabling this mode will automatically add a key for this service to your app's Info.plist.

When this capability is enabled, iOS will allow your app to be woken up periodically. However, it won't magically work right away. In the application(_:didFinishLaunchingWithOptions:) method of AppDelegate, you must inform the system that you want the app to become active every once in a while and also specify the minimum time between fetches.

Once you've done this, iOS will periodically wake your app up. When it does, the application(_:performFetchWithCompletionHandler:) method is called. This method should be implemented in AppDelegate, and it's expected to call the provided completion handler once the work is completed. If you fail to call this method in time, iOS will make a note of this, and missing the window to call this completion-handler could result in your app not being woken up as often as you'd like, so make sure to always call it when the background work completes.

After calling the completion-handler, your app will be put back to sleep until the user opens the app or until the next time your app is allowed to perform a background fetch. Calling the completion-handler shouldn't be done until all of the work that you intended to do is done because once the completion-handler is called, there are no guarantees about how and when your app will make a transition to the background. The only thing you know for sure is that it will happen soon.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.151.126