Face ID userFallback在iOS Swift中不起作用

时间:2018-07-31 04:47:12

标签: ios swift fallback face-id

我的iOS应用中有一个正在实施的人脸ID。当身份验证失败时,将显示一个警告,其中包含“取消”和“重试”按钮,“取消”按钮有效,但“重试”按钮不起作用。我该如何编辑此按钮或要再次尝试使用该按钮。当我单击重试按钮时,如果我单击正在运行的取消按钮,即使取消按钮的标题也发生了变化,但没有任何动作应用在重试上,则没有任何反应。我尝试了以下代码,并在互联网上进行了大量搜索但没有发现。 enter image description here

func startFaceIDTest(){
    attempted = true
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        let reason = "Identify yourself!"
        context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { 
            successfaceID, authenticationError in

            DispatchQueue.main.async {
                if successfaceID {
                    self.success = true
                } else {
                    self.success = false

                    guard let errorr = authenticationError else {return}

                    switch(errorr) {
                        case LAError.authenticationFailed:
                            print("Failed")
                            break
                        case LAError.userCancel:
                            print("User cancel")
                            break
                        case LAError.userFallback:
                            print("Fallback")
                            break
                        case LAError.systemCancel:
                            print("System cancel")
                            break
                        default:
                            break
                    }
                }
            }
        })
    } else {
        if let err = error {
            if #available(iOS 11.0, *) {
                self.success = false
                switch err.code {
                    case LAError.Code.biometryNotEnrolled.rawValue:
                        notifyUser("Your device not enrolled for biometric",
                                   err: err.localizedDescription)

                    case LAError.Code.passcodeNotSet.rawValue:
                        notifyUser("A passcode has not been set",
                                   err: err.localizedDescription)

                    case LAError.Code.biometryNotAvailable.rawValue:
                        notifyUser("Biometric authentication not available",
                                   err: err.localizedDescription)

                    default:
                        notifyUser("Unknown error",
                                   err: err.localizedDescription)
                }
            } else {
                // Fallback on earlier versions
            }
        }
    }
}

func notifyUser(_ msg: String, err: String?) {
    let alert = UIAlertController(title: msg, message: err, preferredStyle: .alert)

    let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)

    alert.addAction(cancelAction)

    self.present(alert, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

func bioandpasscodeAuthentication() {
    let context = LAContext()
    var error:NSError?

    // edit line - deviceOwnerAuthentication
    guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else {
        //showAlertViewIfNoBiometricSensorHasBeenDetected()
        return
    }

      let reason = "Identify yourself!"

    // edit line - deviceOwnerAuthentication
    if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {

        // edit line - deviceOwnerAuthentication
        context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason, reply: { (success, error) in
            if success {
                DispatchQueue.main.async {
                    print("Authentication was successful")
                }
            }else {
                DispatchQueue.main.async {
                    //self.displayErrorMessage(error: error as! LAError )
                    print("Authentication was error")
                }
            }
        })
    }else {
        // self.showAlertWith(title: "Error", message: (errorPointer?.localizedDescription)!)
    }
}

尝试此代码