2017-07-03 17 views
2

CallKitでコールを受信して​​から12秒後にタイムアウトTimerを追加しようとしましたが、アプリがバックグラウンドのときに解雇されません。Appdelegate私のコード:時間の経過後のコールタイムアウト機能

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!) 
     } 
    } 

私が間違っていることは何ですか?

答えて

0

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 
    } 
} 
下にしてみてください
関連する問題