Background fetch is a new mode that lets your app appear always up-to-date with the latest information while minimizing the impact on battery. You could download feeds within fixed time intervals with this capability.
To get started:
1- Check Background Fetch in capabilities screen in Xcode.
2- In application(_:didFinishLaunchingWithOptions:)
method in AppDelegate
, add:
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
[[UIApplication shared] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]
Instead of
UIApplicationBackgroundFetchIntervalMinimum
, you could use anyCGFloat
value to set fetch intervals.
3- You must implement application(_:performFetchWithCompletionHandler:)
. Add that to your AppDelegate
:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// your code here
}