// 导入头文件
#import <LocalAuthentication/LocalAuthentication.h>
- (void)userFigerprintAuthenticationTipsStr:(NSString *)tipsStr{
//初始化上下文对象
LAContext *context = [[LAContext alloc] init];
// 验证失败后系统弹出框右边的按钮title默认为输入密码
context.localizedFallbackTitle = @"默认为输入密码";
NSError *err = nil;
// 判断设备是否支持指纹解锁
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&err]) {
//支持指纹验证
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:tipsStr reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"验证成功");
}else{
/**********************************************************************
/// Authentication was not successful, because user failed to provide valid credentials.
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
/// Authentication was canceled by user (e.g. tapped Cancel button).
LAErrorUserCancel = kLAErrorUserCancel,
/// Authentication was canceled, because the user tapped the fallback button (Enter Password).
LAErrorUserFallback = kLAErrorUserFallback,
/// Authentication was canceled by system (e.g. another application went to foreground).
LAErrorSystemCancel = kLAErrorSystemCancel,
/// Authentication could not start, because passcode is not set on the device.
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
/// Authentication could not start, because Touch ID is not available on the device.
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
/// Authentication could not start, because Touch ID has no enrolled fingers.
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
/// Authentication was not successful, because there were too many failed Touch ID attempts and
/// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating
/// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout,
/// Authentication was canceled by application (e.g. invalidate was called while
/// authentication was in progress).
LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,
/// LAContext passed to this call has been previously invalidated.
LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
*********************************************************************/
// 验证失败的几种情况
switch (error.code) {
// 授权失败
case LAErrorAuthenticationFailed:
NSLog(@"LAErrorSystemCancel");
break;
// 用户取消
case LAErrorUserCancel:
NSLog(@"LAErrorUserCancel");
break;
// 验证失败
case LAErrorUserFallback:
NSLog(@"LAErrorUserFallback");
break;
// 切换到其他APP,系统取消验证Touch ID
case LAErrorSystemCancel:
NSLog(@"LAErrorAppCancel");
break;
// 系统未设置密码
case LAErrorPasscodeNotSet:
NSLog(@"LAErrorSystemCancel");
break;
// 设备Touch ID不可用,例如未打开
case LAErrorTouchIDNotAvailable:
NSLog(@"LAErrorTouchIDNotAvailable");
break;
// 设备Touch ID不可用,用户未录入
case LAErrorTouchIDNotEnrolled:
NSLog(@"LAErrorTouchIDNotEnrolled");
break;
case LAErrorTouchIDLockout:
NSLog(@"LAErrorTouchIDLockout");
break;
case LAErrorAppCancel:
NSLog(@"LAErrorAppCancel");
break;
case LAErrorInvalidContext:
NSLog(@"LAErrorAppCancel");
break;
default:
break;
}
}
}];
}else{
NSLog(@"不支持touch id");
}
}
附:我的博客地址
网友评论