Fetch content opportunistically in the background and update your app's interface.
Background App Refresh lets your app run periodically in the background so that it can update its content. Apps that update their content frequently, such as news apps or social media apps, can use this feature to ensure that their content is always up to date. Downloading data in the background before it is needed minimizes the lag time in displaying that data when the user launches the app.
To support Background App Refresh for your app, do the following:
Enable the background fetch capability in your app ().
application:method in your app delegate.
perform Fetch With Completion Handler:
When the system calls your app delegate’s
application: method, configure a
NSURLSession object to download any new data. The system waits until network and power conditions are good, so you should be able to retrieve adequate amounts of data quickly. When you finish updating your app, call the completion handler and provide an accurate result of your outcome, which can include saying that no new data was available.
Listing 1 shows how to request and handle background updates. The Xcode project for the app enables the background fetch capability and the app requests hourly updates at launch time. When it receives execution time, the app checks to see if new data is available. If it is, the app adds that data to its main feed.
If the absence of Background App Refresh would have a significant impact on your app’s user experience, you can check the
background property of
UIApplication to determine whether the feature is available. Users can disable Background App Refresh for your app or for all apps in Settings. If you prompt the user to enable the feature for your app, respect the user’s decision and don’t prompt a second time.