Asking iOS to wake your app

You're halfway there in the process of enabling background fetching for MustC. All that's left is inform iOS that it should wake the app up every once in a while. This behavior should be implemented in the application(_:didFinishLaunchingWithOptions:) method in AppDelegate. You must ask iOS to wake your app up in this method because you should ask iOS to wake your app up every time your app launches. When your app transitions from the foreground to the background normally, iOS will know that your app should wake up at certain times. However, if the app is killed entirely, iOS won't be able to wake the app up again, so you must ask iOS to wake your app up again as soon as the app launches again.

There isn't much code involved in enabling background fetching for your app. Add the following line to application(_:didFinishLaunchingWithOptions:):

application.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)

This line will ask iOS to wake the app up at a minimum interval. This means that the app is woken up as often as iOS will allow it to be woken up. It's impossible to predict how often this will be since iOS throttles this interval as it sees fit. If your app often has new data available or is used many times throughout the day, the odds are that your app will be woken up more often than it would be if you rarely have new data available or if the user opens your app once every couple of days.

Alternatively, you could set the fetch interval to UIApplication.backgroundFetchIntervalNever if you want to prevent your app from being woken up at all, or you can use a custom value. If you provide a custom value, iOS will attempt to honor your custom interval, but again, there are no guarantees with background fetch because the system remains in charge concerning the intervals that are used.

The only thing iOS does guarantee is that your app is not woken up more often than the value you specified. So, let's say you define a custom interval for your app to be woken up once every three hours, iOS won't wake your app more often than that, but it's possible that your app is woken up only once every eight hours. This depends on several factors that are not transparent to you as a developer.

Your app is now ready to fetch data in the background. Let's go ahead and implement the fetching of data.

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

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