持续时间Callkit后调用超时功能

时间:2017-07-03 09:31:39

标签: ios ios10 callkit

我在收到Timer的来电后尝试添加超时CallKit 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!)
        }
    }

任何想法我做错了什么?

1 个答案:

答案 0 :(得分: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
    }
}
相关问题