1

我已按照本教程在我的应用程序上设置身份验证。它工作正常,但是当尝试终止应用程序时,我得到以下信息:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“在从主线程访问布局引擎后,不得从后台线程对其进行修改。” * First throw call stack: (0x18bf22a48 0x18bc49fa4 0x18c3f8f08 0x18c1fa03c 0x19035664c 0x190357a00 0x18f604c5c 0x18f6004c8 0x18f600734 0x18f600a54 0x18f6054dc 0x18f605328 0x18f5e7004 0x18f97b134 0x18f97b838 0x18f990f70 0x18f989d7c 0x18f98b790 0x18f98dc6c 0x18f98e168 0x18f98dbbc 0x18f98de24 0x100c0f4ec 0x100c01780 0x1017917fc 0x101792bd8 0x1017952d4 0x1017a4160 0x1017a4a88 0x18bc3eb48 0x18bc41760) libc++abi.dylib: terminating with uncaught exception NSException (lldb) 类型

我不明白这个错误。

这是我的代码:

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 {
                    DispatchQueue.main.async {
                        let ac = UIAlertController(title: "Errore", message: "Riprova", preferredStyle: .alert)
                        ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                            self?.authenticateUser()
                            return
                        }))
                        self?.present(ac, animated: true, completion: nil)
                    }
                }
            }
        }
    } else {
        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

            DispatchQueue.global().async {
                if success {
                    DispatchQueue.main.async {
                        self.loginSuccessfull()
                    }
                } else {
                    let ac = UIAlertController(title: "Authentication failed", message: "Sorry!", preferredStyle: .alert)
                    ac.addAction(UIAlertAction(title: "OK", style: .default))
                    self.present(ac, animated: true)
                }
            }
        }
    } else {
        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))
        present(ac, animated: true)
    }
}

如您所见,我只是评估生物特征。再说一遍:这在应用程序的整个“生命周期”中都能完美运行,但是当我终止应用程序时问题就出现了。它只是崩溃并让我进入主屏幕。

设置>隐私...中没有崩溃日志

4

1 回答 1

2

请用于身份验证功能,它是一个全局功能

//enum for response handler

enum AuthenticatinsError: String {
    case userEnrolled = "User is not enrolled"
    case passCodeNotSet = "user not set passcode"
    case biometricNotAvelabel = "Biometric authentication not available"
    case faild  = "faild to authenticat"
    case noIssue = ""

}

func authenticationUser(compleation: @escaping (_ status: Bool, _ msgg: AuthenticatinsError) -> Void) {
    context = LAContext()
    //check inside app if biometric on then in UserDefault set true other wise false
    //let isBiometricOn = WUserDefault.getBiometric()
    //if isBiometricOn == false {
    //context.invalidate()
    //}

    // First check if we have the needed hardware support.
    var error: NSError?
    if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {

        let reason = "Log in to your account"
        context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in

            if success {
                compleation(true, .noIssue)
            } else {
                print(error?.localizedDescription ?? "Failed to authenticate")
                compleation(false, .faild )
            }
        }
    } else {
        if let err = error {
            if #available(iOS 11.0, *) {
                switch err.code {

                case LAError.Code.biometryNotEnrolled.rawValue:
                    notifyUser("User is not enrolled",
                               err: err.localizedDescription)
                    compleation(false, .userEnrolled)

                case LAError.Code.passcodeNotSet.rawValue:

                    compleation(false, .passCodeNotSet)

                case LAError.Code.biometryNotAvailable.rawValue:
                    notifyUser("Biometric authentication not available",
                               err: err.localizedDescription)
                    compleation(false, .biometricNotAvelabel)
                default:
                     compleation (false, .passCodeNotSet)
                }
            } else {
                // Fallback on earlier versions
            }

        }
    }
}

您调用的用户代码

self.authenticationUser { (statu, msg) in
                if statu == true {
                    DispatchQueue.main.async {
                         self?.loginSuccessfull()
                    }
                } else {
                    DispatchQueue.main.async {
                    let ac = UIAlertController(title: "Errore", message: "Riprova", preferredStyle: .alert)
                    ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                        self?.authenticateUser()
                        return
                    }))
                    self?.present(ac, animated: true, completion: nil)
                }
                }

            }
于 2020-01-04T10:08:29.963 回答