IOS实现TouchID和FaceID

时间:2022-06-01 12:58:18

IOS TouchID或FaceID核心实现

1.#import <LocalAuthentication/LocalAuthentication.h>

2.创建LAContext实例context

3.配置context的localizedFallbackTitle,为验证失败后的撤销操作

4.主要实现context的两个方法:

       (1)- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));

用于验证手机是否支持FaceID或TouchID

         (2)   - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason
reply:(void(^)(BOOL success, NSError * __nullable error))reply;

用于启动TouchID或FaceID验证

5.之后是处理验证后的回调操作,判断success确认验证是否通过,error.code来判断验证失败原因:

LAErrorSystemCancel:系统取消授权

LAErrorUserCancel:用户取消验证

LAErrorAuthenticationFailed:验证失败

LAErrorPasscodeNotSet:密码未设置

kLAErrorTouchIDNotAvailable:Touch ID尚未启用

kLAErrorTouchIDNotEnrolled:Touch ID尚未录入


代码:

//
// ViewController.m
// TouchIDDemo
//
// Created by dave.luo on 2018/6/14.
// Copyright © 2018年 dave.luo. All rights reserved.
//
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize context;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
context = [LAContext new];
context.localizedFallbackTitle = @"忘记密码";
_result.hidden=YES;
[self verifyTouchID];
}
-(void)verifyTouchID{
NSError *error = nil;
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
NSLog(@"支持指纹识别");
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指纹解锁" reply:^(BOOL success, NSError * _Nullable error) {
if(success){
NSLog(@"验证成功");
dispatch_async(dispatch_get_main_queue(), ^{
_result.hidden=NO;
_result.text = @"验证成功";
});
}
else{
NSLog(@"%@",error.localizedDescription);
dispatch_async(dispatch_get_main_queue(), ^{
_result.hidden=NO;
_result.text = @"验证失败";
});
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"系统取消授权");
break;
}
case LAErrorUserCancel:{
NSLog(@"用户取消验证");
break;
}
case LAErrorAuthenticationFailed:{
NSLog(@"验证失败");
break;
}
case LAErrorPasscodeNotSet:{
NSLog(@"密码未设置");
break;
}
case kLAErrorTouchIDNotAvailable:{
NSLog(@"Touch ID尚未启用");
break;
}
case kLAErrorTouchIDNotEnrolled:{
NSLog(@"Touch ID尚未录入");
break;
}
case kLAErrorUserFallback:{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"用户选择输入密码,切换主线程处理");
}];
break;
}
default:{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其他情况,切换主线程处理");
}];
break;
}
}
}
}];
}else{
NSLog(@"不支持指纹识别的设备");
dispatch_async(dispatch_get_main_queue(), ^{
_result.hidden=NO;
_result.text = @"不支持指纹识别的设备";
});
switch (error.code) {
case kLAErrorTouchIDNotEnrolled:
{
NSLog(@"TouchID is not enrolled");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"A passcode has not been set");
break;
}
default:
{
NSLog(@"TouchID not available");
break;
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end