I've been trying to implement background fetch in my app for iOS 13 and 14.x Here's the steps I followed
- Enable Background fetch mode in the project capabilities
- In the info.plist, add an array for key
BGTaskSchedulerPermittedIdentifiers
with the relevant id for bkg fetch task - In the
AppDelegate
didFinishLaunchingWithOptions:
, register the background task
BGTaskScheduler.shared.register(forTaskWithIdentifier: bkgTaskId, using: nil) { (task) in
self.handleAppRefreshTask(task: task as! BGAppRefreshTask)
}
handleAppRefreshTask(task: BGAppRefreshTask)
handles the background task. It has theexpirationHandler
set and marks task completed once doneAt the end of the above method, schedule the task to be fired after an interval of 1 hour (or whenever iOS schedules it)
func scheduleRefresh() {
let request = BGAppRefreshTaskRequest(identifier: bkgTaskId)
request.earliestBeginDate = Date(timeIntervalSinceNow: 3600)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
Log("Could not schedule app refresh: \(error)")
}
}
- In
applicationDidEnterBackground:
, callscheduleRefresh
to schedule the background task
When I simulate the background fetch task from Xcode (running the app on a device), I get the below in the console and the background task is never scheduled
[ProcessSuspension] 0x28138ef40 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because RunningBoard has already started the expiration timer
Anything on what I might be doing wrong or how to fix this would be very helpful. Thanks.