UIApplication.shared.beginBackgroundTask is not working on iOS 13. Is there any alternative to implement long running background tasks on iOS 13? Also, it works perfectly on iOS 12 and below versions.
When the app goes background, it is getting terminated within 2 minutes whereas I want it to continue for hours in the background as we are doing some processing in the background.
Below is the code that I am using so that when my app is in background, it can get extra execution time. We need that extra execution time because we are sending the current location to the server on periodic intervals.
/// Register background task. This method requests additional background execution time for the app
private func registerBackgroundTask() {
DispatchQueue.global().async {
self.backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "BgTask", expirationHandler: {
// Ends long-running background task
self.endBackgroundTask()
})
}
}
/// Ends long-running background task. Called when app comes to foreground from background
private func endBackgroundTask() {
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}