Reputation: 16446
I am using call kit and voip calling. I have case when user is in call and user terminate app from background then I wanted to perform some small operations like leave the calling channel, save the call etc.
I have tried following thing but seems not working for me.
var backgroundTask: UIBackgroundTaskIdentifier = .invalid
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
if self.status == .inCall {
self.backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {[unowned self] in
NSLog("BACKGROUND TIME REMAINING %f", UIApplication.shared.backgroundTimeRemaining)
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = .invalid
})
if let topVC = UIApplication.shared.topMostViewController as? CallingController {
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {// this line for just testing
topVC.sendText(string: Constants.ChannelMessages.endCall)
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {// after two seconds leave the channel and save record
topVC.leaveChannelSaveRecord(completion: {
AppDelegate.shared.callManager.endCall(call: topVC.call)
// Let's wait for 2 seconds and end background task
// DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = .invalid
// })
})
})
})
}
}
}
I have already enabled following
Any help or suggestion would be appreciated
thanks in advance
Upvotes: 1
Views: 2581
Reputation: 43
References : https://www.raywenderlich.com/5817-background-modes-tutorial-getting-started
Enable background fetch in capality.
Did enter background enable keeplive
This is a solution for background mode.
Upvotes: -1