我正在使用通话套件和 voip 通话。当用户正在通话并且用户从后台终止应用程序时,我遇到了一些情况,然后我想执行一些小操作,例如离开通话频道,保存通话等。
我已经尝试过以下事情,但似乎对我不起作用。
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
// })
})
})
})
}
}
}
我已经启用了以下
任何帮助或建议将不胜感激
提前致谢