持续时间后的呼叫超时功能Callkit

问题描述:

我试图在接收CallKit的呼叫后12秒增加一个超时Timer,但当应用处于后台时,它不会在Appdelegate中被触发。我的代码:持续时间后的呼叫超时功能Callkit

self.callBackgroundHandlerIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: { 
      UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!) 
      self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid 
     }) 
     DispatchQueue.global(qos: .userInitiated).async { 
      AppDelegate.callAnswerTimer = Timer.scheduledTimer(timeInterval: 12, target: self, selector: #selector(AppDelegate.hangUpOnCallTimeOut), userInfo: nil, repeats: false) 

      self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid 
     } 

func hangUpOnCallTimeOut(){ 
     AppDelegate.timeOutTimer?.invalidate() 
     AppDelegate.timeOutTimer = nil 
     if #available(iOS 10.0, *) { 
      ProviderDelegate.sharedInstance.endCall(uuids: ApplicationDelegate.activeCalls!) { (uuid) in } 
     } 
     if self.callBackgroundHandlerIdentifier != UIBackgroundTaskInvalid { 
      UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!) 
     } 
    } 

任何想法我做错了什么?

a)不要在背景上的块中添加一个计时器。在主队列上运行定时器!

B)不复位self.callBackgroundHandlerIdentifier为时尚早。

:)试试下面的代码


func ... { 
    self.callBackgroundHandlerIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: { 
     print("expired!") 
     UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!) 
     self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid 
    }) 

    AppDelegate.callAnswerTimer = Timer.scheduledTimer(timeInterval: 12, target: self, selector: #selector(AppDelegate.hangUpOnCallTimeOut), userInfo: nil, repeats: false) 
    } 
} 

func hangUpOnCallTimeOut(){ 
    AppDelegate.timeOutTimer?.invalidate() 
    AppDelegate.timeOutTimer = nil 
    if #available(iOS 10.0, *) { 
     ProviderDelegate.sharedInstance.endCall(uuids: ApplicationDelegate.activeCalls!) { (uuid) in } 
    } 
    if self.callBackgroundHandlerIdentifier != UIBackgroundTaskInvalid { 
     UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!) 
     self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid 
    } 
}