前言:
随着指纹解锁的普及,越来越多的应用开始使用指纹解锁、指纹支付.不过作为iOS开发,这倒不难,因为苹果为我们封装好了框架,我们只需调用接口就可以啦!
首先 导入```#import <LocalAuthentication/LocalAuthentication.h>
//创建LAContext对象
LAContext *context = [LAContext new];
//设置指纹输入失败之后的弹出框的属性
context.localizedFallbackTitle = @"选择手动输入密码";
NSError *error = nil;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
NSLog(@"此设备支持指纹识别");
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请按home键进行指纹解锁" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
//解锁成功,进入页面
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"验证成功 刷新主界面");
}];
}else{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"系统取消授权,其他app切入等等");
break;
}
case LAErrorUserCancel:
{
NSLog(@"用户取消验证Touch ID");
// 用户不愿使用指纹解锁,换用其他操作
break;
}
case LAErrorAuthenticationFailed:
{
NSLog(@"授权失败");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"系统未设置密码");
break;
}
case LAErrorTouchIDNotAvailable:
{
NSLog(@"设备Touch ID不可用,例如未打开");
break;
}
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"设备Touch ID不可用,用户未录入");
break;
}
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"用户选择输入密码,切换主线程处理");
}];
break;
}
default:
{
break;
}
}
}
}];
}else{
NSLog(@"不支持指纹识别");
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"TouchID is not enrolled");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"A passcode has not been set");
break;
}
default:
{
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
}
}
网友评论