【问题标题】:call timeout function after duration Callkit持续时间Callkit后调用超时功能
【发布时间】:2017-07-03 09:31:39
【问题描述】:

在收到CallKit 的电话后,我尝试在Timer 12 秒后添加超时,但当应用程序处于后台时,它不会在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!)
        }
    }

任何想法我做错了什么?

【问题讨论】:

  • 嗨哥们,你能分享一下关于通话超时的完整代码

标签: ios ios10 callkit


【解决方案1】:

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

【讨论】:

  • 你好,你能否发布更多关于 appdelegate 的代码以提供“timeOutTimer”、“callAnswerTimer”
猜你喜欢
  • 2018-06-02
  • 2018-02-08
  • 1970-01-01
  • 2016-08-20
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
相关资源
最近更新 更多