TouchID获取错误类型' LAError'没有会员' UserCancel'

时间:2016-10-03 13:13:54

标签: swift3 ios10 xcode8

我想要启用touchId,但我收到错误:

  

键入' LAError'没有会员' UserCancel' "

我能发现它是LAError的成员我错过了什么? 这就是Xcode给我的东西。

  

键入' LAError'没有会员' AuthenticationFailed'   
类型' LAError'没有会员' UserCancel'   
类型' LAError'没有会员' UserFallback'

这是我的代码:

func authenticateUser()
{
    let context = LAContext()
    var error: NSError?
    let reasonString = "Authentication is needed to access your app! :)"

    if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error)
    {
        context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (

            success, Error) -> Void in

            if success
            {
                print("Authentication successful! :) ")
            }
            else
            {
                    switch(error!._code)                 {
                case LAError.AuthenticationFailed.rawValue:
                    print("Authentication was cancelled by the system.")
                case LAError.UserCancel.rawValue:
                    print("Authentication was cancelled by the user.")

                case LAError.UserFallback.rawValue:
                    print("User selected to enter password.")
                    OperationQueue.main.addOperation({ () -> Void in
                        self.showPasswordAlert()
                    })
                default:
                    print("Authentication failed! :(")
                    OperationQueue.main.addOperation({ () -> Void in
                        self.showPasswordAlert()
                    })
                }
            }

        })
    }
    else
    {
        print(error?.localizedDescription)
        OperationQueue.main.addOperation({ () -> Void in
            self.showPasswordAlert()
        })
    }
}

// MARK: Password Alert

func showPasswordAlert()
{
    let alertController = UIAlertController(title: "Touch ID Password", message: "Please enter your password.", preferredStyle: .alert)

    let defaultAction = UIAlertAction(title: "OK", style: .cancel) { (action) -> Void in

        if let textField = alertController.textFields?.first as UITextField?
        {
            if textField.text == "veasoftware"
            {
                print("Authentication successful! :) ")
            }
            else
            {
                self.showPasswordAlert()
            }
        }
    }
    alertController.addAction(defaultAction)

    alertController.addTextField { (textField) -> Void in

        textField.placeholder = "Password"
        //textField.secureTextEntry = true

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

1 个答案:

答案 0 :(得分:1)

快速

这是我的解决方法

import UIKit
import LocalAuthentication

func authendicateTouchID() {
        let context: LAContext = LAContext()
        let myLocalizedReasonString = "Authentication is needed to access your Home ViewController."

        var authError: NSError?
        if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
            context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: myLocalizedReasonString, reply: { success, error in
                if success {
                    DispatchQueue.main.async {
                    print("Authentication success by the system")
                    }
                }
                else {
                    if let error = error {
                        let message = self.showErrorMessageForLAErrorCode(errorCode: error as! Int)
                    print(message)
                    }
                }

            })
        }
    }

    func showErrorMessageForLAErrorCode( errorCode:Int ) -> String{

        var message = ""

        switch errorCode {

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

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

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

        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.touchIDLockout.rawValue:
            message = "Too many failed attempts."

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

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

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

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

        }

        return message

    }

    @IBAction func buttonFingerPrint(_ sender: Any) {
        self.authendicateTouchID()
    }