1

我在我的 iOS 应用程序中实现了人脸 ID。当身份验证失败时,会显示包含取消并重试按钮的警报,取消按钮有效但重试按钮不起作用。我怎样才能编辑这个按钮或工作重试按钮。当我单击重试按钮时,如果我单击正在工作的取消按钮,甚至取消按钮的标题也在更改,但重试时没有应用任何操作,则没有任何反应。我尝试了以下代码并在互联网上进行了大量搜索但什么也没找到。 在此处输入图像描述

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

1 回答 1

-1
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)!)
    }
}

试试这个代码

于 2018-07-31T05:54:46.887 回答