ios - 持续时间 Callkit 后调用超时函数

标签 ios ios10 callkit

我尝试在收到 CallKit 的调用后 12 秒添加超时 Timer 但当应用程序在后台。我的代码:

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) 不要在后台的 block 中添加计时器。在主队列上运行计时器!

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

关于ios - 持续时间 Callkit 后调用超时函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44882154/

相关文章:

ios - 如何为最近的原生 ios 应用程序设置名称字段? - iOS

javascript - 如何在xcode 5.1.1中使用phonegap-1.3.0.js实现相机功能

ios - 隐藏奇怪的不需要的 Xcode 日志

ios - CallKit 干扰 WebRTC 视频

iOS 10 语音识别 `kAFAssistantErrorDomain` 错误代码

ios - 替代 iOS 10.0 核心数据中的 NSPersistentStoreUbiquitousContentNameKey 键

ios - IOS 中的 CallKit 扩展

ios - 如何在 iOS 中为一组图像制作动画

c++ - 如何将NSData转换成OpenCV的图像(Mat)

ios - 如何快速调整大图像的大小并使其适合 UIButton?