iOS Touch ID

// 导入头文件
#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");
    }
}
复制代码

附:我的博客地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值