Touch ID 简单使用

Touch ID 指纹识别首次应用是在2013年9月发布的iPhone 5s 上。在今天,指纹识别已经是所有智能手机必不可少的一部分。它不仅能保证手机中信息的安全,管理自己的隐私,还能够快捷快速的进行密码输入等操作。在保证安全的同时也提高了使用的效率。
虽说Touch ID 诞生在iPhone 5s,但是在iOS 8 中才正式开放API。而且随着手机硬件的升级,Touch ID 的识别效率大大提高,这使得越来越多的App加入了指纹解锁的功能。
LAContext提供了关于指纹识别的接口。这里有几个主要的方法和属性:

canEvaluatePolicy 方法来判断设备是否支持指纹识别。
evaluatePolicy 方法来进行指纹识别。
invalidate 方法主动让指纹识别上下文失效。 (iOS 9+)
localizedFallbackTitle 属性 设置验证第一次失败后显示按钮的标题。
localizedCancelTitle 属性设置取消验证按钮的标题。

使用

在iOS 中加入Touch ID 指纹非常简单,只需要几句代码就能搞定,

首先需要引入LocalAuthentication

import LocalAuthentication

初始化验证上下文

let context = LAContext()
// 在第一次验证失败之后,可以提供别的方式的入口
context.localizedFallbackTitle = "使用密码登录"

进行验证,苹果提供了一个LAError 来捕捉Touch ID 验证过程中可能会出现的错误,我们可以针对错误类型来进行预期处理。

var contentError: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &contentError) {
    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "按下你的手指", reply: { (success, error) in
        if success {
            print("验证成功")
        } else {
            if let error = error as? LAError {
                switch error.code {
                /// Authentication was not successful, because user failed to provide valid credentials.
                case .authenticationFailed:
                    print("连续3次验证失败")
                /// Authentication was canceled by user (e.g. tapped Cancel button).
                case .userCancel:
                    print("用户取消验证")
                /// Authentication was canceled, because the user tapped the fallback button (Enter Password).
                case .userFallback:
                    OperationQueue.main.addOperation {
                    print("用户选择输入密码,切换主线程处理")
                    }
                /// Authentication was canceled by system (e.g. another application went to foreground).
                case .systemCancel:
                    print("系统取消")
                /// Authentication could not start, because passcode is not set on the device.
                case .passcodeNotSet:
                    print("未设置密码")
                /// Authentication could not start, because Touch ID is not available on the device.
                case .touchIDNotAvailable:
                    print("硬件问题导致不可用")
                /// Authentication could not start, because Touch ID has no enrolled fingers.
                case .touchIDNotEnrolled:
                    print("手指未登记")
                /// Authentication was not successful, because there were too many failed Touch ID attempts and
                /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating
                /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
                ///  iOS 9+
                case .touchIDLockout:
                    print("TouchID锁定")
                /// Authentication was canceled by application (e.g. invalidate was called while
                /// authentication was in progress).
                ///  iOS 9+
                case .appCancel:
                    print("程序退出")
                /// LAContext passed to this call has been previously invalidated.
                /// iOS 9+
                case .invalidContext:
                    print("无效的验证上下文")
                }
            }
        }
    })
}

你可能感兴趣的:(Touch ID 简单使用)