如何显示输入密码并再次尝试提醒视图

时间:2018-03-27 07:09:38

标签: swift xcode touch-id

所以我想进行touchid身份验证,但如果touchid不匹配,则会有提示输入密码并重试

我正在使用XCode 9.0.0

@IBAction func ac(_ sender: Any) {

    let context:LAContext=LAContext()
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil){

        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "use your touch id", reply: {(wasCorrect,error) in

            if wasCorrect {

                print("correct")
            }else{

                print("incorrect")
            }


        })
    }else{


    }
}

4 个答案:

答案 0 :(得分:1)

最好的方法(从用户的角度来看)是启用/显示"重新验证"如果身份验证失败,则按钮然后,用户必须主动点击该按钮以重新运行autentication循环。

如果您自动开始重新验证,用户可能会因为一直弹出的身份验证对话框而感到恼火,因为有时动画会重叠并且屏幕似乎闪烁,因此可能看起来有点烦人。

答案 1 :(得分:1)

这将有效,为swift 3和4更新

 func authenticationWithTouchID() {
    let localAuthenticationContext = LAContext()
    localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

    var authError: NSError?
    let reasonString = "To access the secure data"

    if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {

        localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in

            if success {

                //TODO: User authenticated successfully, take appropriate action

            } else {
                //TODO: User did not authenticate successfully, look at error and take appropriate action
                guard let error = evaluateError else {
                    return
                }
                let message = self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code)

                let alert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))

                self.presentViewController(alert, animated: true, completion: nil)



            }
        }
    } else {

        guard let error = authError else {
            return
        }
         let message = self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code)

                let alert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))

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

以及获取错误消息的方法

func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
    var message = ""
    if #available(iOS 11.0, macOS 10.13, *) {
        switch errorCode {
            case LAError.biometryNotAvailable.rawValue:
                message = "Authentication could not start because the device does not support biometric authentication."

            case LAError.biometryLockout.rawValue:
                message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."

            case LAError.biometryNotEnrolled.rawValue:
                message = "Authentication could not start because the user has not enrolled in biometric authentication."

            default:
                message = "Did not find error code on LAError object"
        }
    } else {
        switch errorCode {
            case LAError.touchIDLockout.rawValue:
                message = "Too many failed attempts."

            case LAError.touchIDNotAvailable.rawValue:
                message = "TouchID is not available on the device"

            case LAError.touchIDNotEnrolled.rawValue:
                message = "TouchID is not enrolled on the device"

            default:
                message = "Did not find error code on LAError object"
        }
    }

    return message;
}

func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {

    var message = ""

    switch errorCode {

    case LAError.authenticationFailed.rawValue:
        message = "The user failed to provide valid credentials"

    case LAError.appCancel.rawValue:
        message = "Authentication was cancelled by application"

    case LAError.invalidContext.rawValue:
        message = "The context is invalid"

    case LAError.notInteractive.rawValue:
        message = "Not interactive"

    case LAError.passcodeNotSet.rawValue:
        message = "Passcode is not set on the device"

    case LAError.systemCancel.rawValue:
        message = "Authentication was cancelled by the system"

    case LAError.userCancel.rawValue:
        message = "The user did cancel"

    case LAError.userFallback.rawValue:
        message = "The user chose to use the fallback"

    default:
        message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
    }

    return message
}

答案 2 :(得分:0)

希望这会有所帮助:

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

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

        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
            [unowned self] success, authenticationError in

            DispatchQueue.main.async {
                if success {
                    self.runSecretCode()
                } 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)
    }
}

更多了解:https://www.hackingwithswift.com/example-code/system/how-to-use-touch-id-to-authenticate-users-by-fingerprint

答案 3 :(得分:0)

       @IBAction func ac(_ sender: Any) {

                let context:LAContext=LAContext()
                if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil){

                    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "use your touch id", reply: {(wasCorrect,error) in

                        if wasCorrect {

                        }else{

                            let myalert = UIAlertController(title: " Password Title", message: "Message ", preferredStyle: UIAlertControllerStyle.alert)

                            myalert.addAction(UIAlertAction(title: "Retry", style: .default) { (action:UIAlertAction!) in
                                print("retry")
                            })
                            self.present(myalert, animated: true)
                        }


                    })
                }else{
// If You Wanna Show Alert 
                      let myalert = UIAlertController(title: "Title", message: "Message ", preferredStyle: UIAlertControllerStyle.alert)

        myalert.addAction(UIAlertAction(title: "Retry", style: .default) { (action:UIAlertAction!) in
            print("retry")
        })
        self.present(myalert, animated: true)

                }
            }