Swift-后台线程使用身份验证使应用程序崩溃

问题描述 投票:0回答:1

我正在尝试向我的应用添加人脸ID /触摸ID /代码,但是终止时遇到了一些麻烦:这是我得到的错误:

无法结束BackgroundTask:不存在标识符为12(0xc)的后台任务,或者它可能已经结束。中断UIApplicationEndBackgroundTaskError()以进行调试。

我尝试按照说明进行操作,但是我不知道该怎么办。在互联网上,我发现这可能是iOS 13引起的错误,但我没有任何运行iOS 12的设备对其进行测试。

我的设置中没有任何崩溃日志。

这里是导致错误的代码:

extension ViewController {
func authenticateUser() {
    let context = LAContext()
    var error: NSError?

    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        let reason = "Identify yourself!"

        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { [weak self] success, authError in
            DispatchQueue.main.async {
                if success {
                    self?.loginSuccessfull()
                } else {

                }
            }
        }
    } else {
        DispatchQueue.main.async {
            let ac = UIAlertController(title: "Errore", message: "Il tuo device non è configurato per l'autenticazione", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "Ok", style: .default))
            self.present(ac, animated: true, completion: nil)
        }
    }

    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        let reason = "Identify yourself!"

        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
            [unowned self] success, authenticationError in
            if success {
                DispatchQueue.main.async {
                    self.loginSuccessfull()
                }
            } else {
                DispatchQueue.main.async {
                    let ac = UIAlertController(title: "Autenticazione fallita", message: nil, preferredStyle: .alert)
                    ac.addAction(UIAlertAction(title: "Riprova", style: .default, handler: { (action) in
                        self.authenticateUser()
                    }))
                    self.present(ac, animated: true)
                }
            }
        }
    } else {
        DispatchQueue.main.async {
            let ac = UIAlertController(title: "Touch ID not available", message: "Your device is not configured for Touch ID.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "Ok", style: .default))
            self.present(ac, animated: true)
        }
    }
 }
}

错误仅在我完全终止应用程序之前出现,并且仅在真实设备上显示。模拟器似乎对此没有任何问题。

ios swift touch-id background-thread face-id
1个回答
0
投票

完成后在上下文中调用invalidate()函数。

© www.soinside.com 2019 - 2024. All rights reserved.