如何在iOS 8中使用Touch ID传感器

Abd*_*ari 8 iphone ios touch-id swift ios8

我对iOS 8感到兴奋的最重要的事情之一是能够在iPhone 5s及更高版本上使用指纹传感器.不幸的是,我无法找到所需的框架,也不知道如何进行身份验证.请帮帮我:

  • 使用Touch ID需要什么框架?
  • 如何使用其方法以及如何验证用户?

我们非常感谢代码示例.

txu*_*ulu 9

更完整的代码片段,快捷风格:

func authenticateUser() {
        // Get the local authentication context.
        let context = LAContext()

        // Declare a NSError variable.
        var error: NSError?

        // Set the reason string that will appear on the authentication alert.
        var reasonString = "Authentication is needed to access your notes."

        // Check if the device can evaluate the policy.
        if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
            [context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in

                if success {

                }
                else{
                    // If authentication failed then show a message to the console with a short description.
                    // In case that the error is a user fallback, then show the password alert view.
                    println(evalPolicyError?.localizedDescription)

                    switch evalPolicyError!.code {

                    case LAError.SystemCancel.toRaw():
                        println("Authentication was cancelled by the system")

                    case LAError.UserCancel.toRaw():
                        println("Authentication was cancelled by the user")

                    case LAError.UserFallback.toRaw():
                        println("User selected to enter custom password")
                        self.showPasswordAlert()

                    default:
                        println("Authentication failed")
                        self.showPasswordAlert()
                    }
                }

            })]
        }
        else{
            // If the security policy cannot be evaluated then show a short message depending on the error.
            switch error!.code{

            case LAError.TouchIDNotEnrolled.toRaw():
                println("TouchID is not enrolled")

            case LAError.PasscodeNotSet.toRaw():
                println("A passcode has not been set")

            default:
                // The LAError.TouchIDNotAvailable case.
                println("TouchID not available")
            }

            // Optionally the error description can be displayed on the console.
            println(error?.localizedDescription)

            // Show the custom alert view to allow users to enter the password.
            self.showPasswordAlert()
        }
    }
Run Code Online (Sandbox Code Playgroud)

资源


Zee*_*Zee 6

本地验证框架提供了用于请求使用触摸ID的用户认证,下面的代码剪断展示了应该如何申请认证.

目标C.

LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myReasonString = @"String explaining why app needs authentication";

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
    [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myReasonString
                            reply:^(BOOL succes, NSError *error) {
            if (success) {
                // User authenticated successfully
            } else {
                // Authenticate failed
            }
        }];
} else {
    // Could not evaluate policy; check authError
}
Run Code Online (Sandbox Code Playgroud)

迅速

let myContext = LAContext()
var authError: NSError?

// Set the reason string that will appear on the authentication alert.
var myReasonString = "String explaining why app needs authentication"

if myContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
    [myContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: myReasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in

        if success {
            // User authenticated successfully
        } else {
            // Authenticate failed
        }
    })]
} else{
    // Could not evaluate policy; check authError
}
Run Code Online (Sandbox Code Playgroud)