Background Fetch
suggest changeBackground fetch lets the system wake your app occasionally so content can be refreshed with less battery use than a repeating timer. iOS chooses the schedule. The minimum interval is a hint, not a fixed period.
To get started:
1- Check Background Fetch in capabilities screen in Xcode.
2- In application(_:didFinishLaunchingWithOptions:) method in AppDelegate, add:
Swift
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
Objective-C
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
UIApplicationBackgroundFetchIntervalMinimum asks for the most frequent fetches the system is willing to make. Any other NSTimeInterval (not CGFloat) is only a minimum hint.
3- You must implement application(_:performFetchWithCompletionHandler:). Add that to your AppDelegate:
Swift
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Fetch on a background queue if the work is slow, then always call the handler.
// If you never call it, the system will stop launching the app for fetch.
completionHandler(.newData)
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents