//导入头文件
#import <LocalAuthentication/LocalAuthentication.h>
//在app启动的时候加载
- (void)evaluateAuthenticate
{
//创建LAContext
LAContext* context = [[LAContext alloc] init];
NSError* error = nil;
NSString* result = @"请验证已有指纹";
//首先使用canEvaluatePolicy 判断设备支持状态
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指纹验证
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"验证成功,主线程处理UI");
}
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:
{
NSLog(@"用户选择输入密码,切换主线程处理");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其他情况,切换主线程处理");
}];
break;
}
}
}
}];
}
else
{
//不支持指纹识别,LOG出错误详情
NSLog(@"不支持指纹识别==%ld",(long)error.code);
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"身份验证无法启动,因为Touch ID没有注册手指");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"身份验证不能启动,因为没有在设备上设置密码");
break;
}
default:
{
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
}
}