I am trying to implement background fetch in my iOS app (it's a simple http call that downloads some data for later processing).
I enabled background fetch in my project capabilities:
Also in AppDelegate I set minimum fetch interval:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
return true
}
And implemented fetch handler:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
do {
try api.downloadInBackground()
} catch {
Debug(self).error(error)
}
completionHandler(.newData)
}
Testing with Debug -> Simulate Background Fetch or custom scheme with enabled background fetch mode work fine and data gets downloaded:
However, when the app is not being debugged background fetch is never invoked. I've read that there's no strict rule when it happens and some time and networking has to be done. However, I've tried leaving device over a weekend (connected to wifi) after I've tried reading some emails browsing, and still nothing happened. What could be wrong?
(At first, I thought it might something to do with a provisioning profile because I did not update it in the apple developer portal, but there's no option to do that, so I suspect that enabling this capability only in the project locally is enough?