iOS Touch ID 身份认证
iOS Touch ID 身份认证
iOS 8 及以后录了指纹的设备可以使用 touch ID 进行身份认证,指纹符合录入的指纹才能认证成功。
步骤
- 导入 LocalAuthentication 框架:
import LocalAuthentication
- 初始化 LAContext 对象:
let context = LAContext()
- 调用 LAContext 对象的
canEvaluatePolicy(_ policy: LAPolicy, error: NSErrorPointer) -> Bool
方法 - 上一步如果返回 false,表示不能进行认证,执行相应的失败操作;如果返回 true,调用 LAContext 对象的
evaluatePolicy(_ policy: LAPolicy, localizedReason: String, reply: @escaping (Bool, Error?) -> Void)
方法,在 reply 中判断是否认证成功来执行相应的操作(如果认证失败,可以获取错误码 code,看看属于 LAError.Code 的哪种类型错误来执行相应的失败操作)
调用 LAContext 对象的 canEvaluatePolicy 和 evaluatePolicy 方法都要传入 LAPolicy 枚举类型的值,目前有两种取值:deviceOwnerAuthenticationWithBiometrics 和 deviceOwnerAuthentication。前一种 deviceOwnerAuthenticationWithBiometrics 是进行指纹认证。后一种 deviceOwnerAuthentication 是 iOS 9.0 及以后才能使用,先进行指纹认证,如果指纹认证失败可以通过输入密码进行认证。
调用 LAContext 对象的 evaluatePolicy 方法会弹出指纹认证对话框。对话框会显示需要进行认证的原因(String),就是 localizedReason 参数的值。对话框有取消按钮,iOS 10.0 及以后可以设置 LAContext 对象的 localizedCancelTitle 的值来改变取消按钮显示的字。如果指纹认证失败,对话框还会显示 fallback 按钮,可以设置 LAContext 对象的 localizedFallbackTitle 的值来改变 fallback 按钮显示的字。
需要注意,evaluatePolicy 方法的 reply 回调不在主线程。如果需要更新 UI 的话,要调用主线程再更新。
代码示例
代码已上传 GitHub:https://github.com/Silence-GitHub/TouchIDDemo
在控制器中放置一个 label 显示认证返回结果。
指纹认证代码
let context = LAContext()
context.localizedFallbackTitle = "Fall back button"
if #available(iOS 10.0, *) {
context.localizedCancelTitle = "Cancel button"
}
var authError: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Localized reason for authentication with biometrics", reply: { (success, evaluateError) in
// NOT in main thread
DispatchQueue.main.async {
if success {
self.label.text = "Success"
// Do something success
} else if let error = evaluateError {
self.label.text = error.localizedDescription
// Deal with error
if let code = LAError.Code(rawValue: (error as NSError).code) {
switch code {
case .userFallback:
print("fall back button clicked")
default:
break
}
}
}
}
})
} else if let error = authError {
label.text = error.localizedDescription
// Deal with error
}
指纹和密码认证代码
if #available(iOS 9.0, *) {
let context = LAContext()
context.localizedFallbackTitle = "Fall back button"
if #available(iOS 10.0, *) {
context.localizedCancelTitle = "Cancel button"
}
var authError: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Localized reason for authentication", reply: { (success, evaluateError) in
// NOT in main thread
DispatchQueue.main.async {
if success {
self.label.text = "Success"
// Do something success
} else if let error = evaluateError {
self.label.text = error.localizedDescription
// When fall back button clicked, user is required to enter PIN. Error code will not be "userFallback"
// Deal with error
}
}
})
} else if let error = authError {
label.text = error.localizedDescription
// Deal with error
}
} else {
let alert = UIAlertController(title: nil, message: "Authentication is available on iOS 9.0 or later", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6397148.html
· 为什么PostgreSQL不自动缓存执行计划?
· 于是转身独立开发者
· C#.Net筑基-泛型T & 协变逆变
· dotnet 代码调试方法
· DbContext是如何识别出实体集合的
· 【Cursor保姆级教程】零基础小白从安装到实战,手把手教你玩转AI编程神器!
· Cursor 实战万字经验分享,与 AI 编码的深度思考
· MySQL查询执行顺序:一张图看懂SQL是如何工作的
· 工作流引擎系统-基于橙单(flowable)的系统开发-流程配置举例
· 用 AI 制作超长视频,保姆级教程!