I am using the new iOS13 background task framework, with the implementation of a BGAppRefreshTask type. My problem is, my device is never calling the task, even after waitin
Here is what I use.
In my AppDelegate, the iOS13 code. Older style fetch not included:
class AppDelegate: UIResponder, UIApplicationDelegate, URLSessionDelegate, UNUserNotificationCenterDelegate {
var backgroundSessionCompletionHandler: (() -> Void)?
var backgroundSynchTask: UIBackgroundTaskIdentifier = .invalid
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.WH.Air-Compare.WayneBGrefresh", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
self.logSB.verbose("iOS 13 Registered for Background duty")
}
} else {
// Fallback on earlier versions
self.logSB.verbose("iOS < 13 Registering for Background duty")
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
}
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler:@escaping () -> Void) {
let dateFormatter = DateFormatter() // format the date for output
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.long
let convertedDate = dateFormatter.string(from: Date())
self.logSB.info("Background URLSession handled at \(convertedDate)")
self.logSB.info("Background URLSession ID \(identifier)")
let config = URLSessionConfiguration.background(withIdentifier: "WayneBGconfig")
let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) -> Void in
// yay! you have your tasks!
self.logSB.info("Background completion handler here with \(downloadTasks.count) tasks")
for i in 0...max(0,downloadTasks.count - 1) {
let description: String = downloadTasks[i].taskDescription!
self.logSB.info("Task ID \(i) is \(description)")
}
}
backgroundSessionCompletionHandler = completionHandler
}
func applicationDidEnterBackground(_ application: UIApplication) {
if #available(iOS 13.0, *) {
scheduleAppRefresh()
} else {
// Fallback on earlier versions
}
}
@available(iOS 13.0, *)
func scheduleAppRefresh() {
logSB.info("Scheduling the AppRefresh in iOS 13 !!")
let request = BGAppRefreshTaskRequest(identifier: "com.WH.Air-Compare.WayneBGrefresh")
request.earliestBeginDate = Date(timeIntervalSinceNow: 2 * 60) // Fetch no earlier than 2 minutes from now
do {
try BGTaskScheduler.shared.submit(request)
} catch {
logSB.error("Could not schedule app refresh: \(error)")
}
}
have break points in the code after submit request and after submitting the request(BGTaskScheduler.shared.submit(request))
then run the command
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.XYZ.PearWeather.backgroundAlerts"]
then registered handler will be called for that identifier. Check POC to see further details.