// 创建指纹验证对象
LAContext *context = [[LAContext alloc] init];
// 创建error
NSError *error = nil;
// 验证设备是否支持touchID
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
// 支持touchID
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"XMGlocalizedReason" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"验证成功--继续处理相关业务(注意线程");
} else {
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"切换到其他APP,系统取消验证Touch ID");
break;
}
case LAErrorUserCancel:
{
NSLog(@"用户取消验证Touch ID");
break;
}
case LAErrorUserFallback:
{
NSLog(@"用户选择输入密码,切换主线程处理");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情况,切换主线程处理
}];
break;
}
}
}
}];
} else {
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
NSLog(@"LAErrorTouchIDNotEnrolled");
break;
case LAErrorPasscodeNotSet:
NSLog(@"LAErrorPasscodeNotSet"); // 此处触发showPasscodeResetAlert方法
break;
default:
NSLog(@"Touch ID is unaviliable");
break;
}
NSLog(@"%@", error.localizedDescription);
}
/*
typedef NS_ENUM(NSInteger, LAError)
{
//授权失败
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
//用户取消Touch ID授权
LAErrorUserCancel = kLAErrorUserCancel,
//用户选择输入密码
LAErrorUserFallback = kLAErrorUserFallback,
//系统取消授权(例如其他APP切入)
LAErrorSystemCancel = kLAErrorSystemCancel,
//系统未设置密码
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
//设备Touch ID不可用,例如未打开
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
//设备Touch ID不可用,用户未录入
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);
*/