IOS 指纹识别,这里整理下项目中用的知识。
IOS 指纹识别现在,在要求安全与效率兼得的时候,普通密码已不能满足我们的要求,指纹识别就这样诞生了。
每个人都有自己专属的指纹,在需要支付等输入密码的地方,我们只需轻轻一按即可,避免了输入密码的繁琐步骤,更加安全,而且妈妈再也不用担心我们忘记密码。
其实,听着高大上,实现起来特别简单,因为苹果已经帮我们封装好了,我们只需要简单的调用就好了。1、首先,我们需要导入头文件:#import <LocalAuthentication/LocalAuthentication.h>2、接着,我们需要判断我们的设备是否支持指纹识别(iPhone5s+,iOS8.0+) 接下来,判断当前用户是否是机主即可,完事,是不是so easy啊。
- (IBAction)biologyAction:(id)sender {LAContext *context = [[LAContext alloc] init];NSError *error = nil;NSString *reason = @"我们需要验证您的指纹来确认您的身份";// 判断设置是否支持指纹识别(iPhone5s+、iOS8+支持)if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){// 指纹识别只判断当前用户是否是机主[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {if(success){NSLog(@"指纹认证成功");}else{NSLog(@"指纹认证失败");NSLog(@"错误码:%zd",error.code);NSLog(@"出错信息:%@",error);// 错误码 error.code// -1: 连续三次指纹识别错误// -2: 在TouchID对话框中点击了取消按钮// -3: 在TouchID对话框中点击了输入密码按钮// -4: TouchID对话框被系统取消,例如按下Home或者电源键// -8: 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码}}];}else{NSLog(@"TouchID设备不可用");NSLog(@"错误码:%zd",error.code);NSLog(@"出错信息:%@",error);}}
下面贴出Swift3.0版对应的代码,原理就不说了,和上面的一样,只是将OC代码翻译成了Swift3.0版而已。
import UIKitimport LocalAuthenticationclass ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()}@IBAction func yanzhengAction(_ sender: AnyObject) {let context = LAContext()let reason = "我们需要您的指纹来验证您的身份"var error:NSError?if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error:&error){context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (isSuc, error) inif isSuc{print("指纹验证成功")}else{print("指纹验证失败")print("错误信息:(error)")// 错误码 error.code// -1: 连续三次指纹识别错误// -2: 在TouchID对话框中点击了取消按钮// -3: 在TouchID对话框中点击了输入密码按钮// -4: TouchID对话框被系统取消,例如按下Home或者电源键// -8: 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码}})}else{print("TouchID设置不支持")print("错误码:(error!.code)")print("错误信息:(error)")}}}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!