当前位置: 首页 > news >正文

iOS实现生物识别

1. info.plist中添加权限申请

<key>NSFaceIDUsageDescription</key>
<string>APP would like to use Face ID</string>
<key>NSBiometricUsageDescription</key>
<string>APP would like to use Touch ID</string>

2. 添加库

Build Phases → Link Binary With Libraries → LocalAuthentication.framework

3. 实现代码

#import <LocalAuthentication/LocalAuthentication.h>

// 检查是否支持认证
-(BOOL) isSupportBiometricCheck {
    LAContext *context = [[LAContext alloc] init];
    NSError *error = nil;
    
    // 检查设备是否支持生物识别认证
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
        return true;
    } else {
        return false;
    }
}

// 认证
// title:标题 eg.生物识别登录
// subTitle:副标题 eg.使用生物识别凭证登录
// negativeText:取消文本 eg.取消
- (void)biometricCheck:(CallSN)cbSn title:(NSString *)title
              subTitle:(NSString *)subTitle
        negativeButton:(NSString *)negativeText {
    LAContext *context = [[LAContext alloc] init];
    
    NSString *localizedReason = [NSString stringWithFormat:@"%@\\n%@", title, subTitle];
    
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication
            localizedReason:localizedReason
                      reply:^(BOOL success, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (success) {
                NSLog(@"认证成功");
            } else {
                NSLog(@"认证失败,原因:%@", error.description);
            }
        });
    }];
}

目前来看LAPolicyDeviceOwnerAuthentication优于LAPolicyDeviceOwnerAuthenticationWithBiometrics,因为LAPolicyDeviceOwnerAuthenticationWithBiometrics在验证失败时,需要重新输入一次锁屏密码或者调用LAPolicyDeviceOwnerAuthentication输入密码才可以再次被正常调用。否则直接返回生物特征已锁定,而引起认证失败。

相关文章:

  • git: 如何查询某个文件或者某个目录的更新历史
  • 服务器之连接简介(Detailed Explanation of Server Connection)
  • 网络编程01 - 速通计网知识点
  • python学习第十四天之机器学习名词介绍
  • RNN复兴!性能反超Transformer,训练速度提升1300倍!
  • 数据结构 栈和队列
  • 本地部署DeepSeek + AnythingLLM 搭建高效安全的个人知识库
  • 突破数据壁垒,动态住宅代理IP在数据采集中的高效应用
  • 系统思考—团队学习
  • SpringBoot的单机模式是否需要消息队列?分布式应用中消息队列如何和服务的发现与注册、配置中心、SpringMVC相配合
  • 有哪些免费的SEO软件优化工具
  • AGI时代的认知重塑:人类文明的范式转移与思维革命
  • Python多进程Logging
  • 金融风控项目-业务基础
  • carbon 加入 GitCode:Golang 时间处理的 “瑞士军刀”
  • C语言基础学习之基本语法
  • C++智能指针的使用
  • 服务器绑定 127.0.0.1 和 0.0.0.0 的区别
  • 深入理解 CSS 层叠上下文
  • 只需三步!5分钟本地部署deep seek——MAC环境
  • 网站站建设建设中页中页/优化设计三年级上册语文答案
  • 物流公司做网站/网络营销seo是什么意思
  • 做网站要学会什么语言/昆明百度推广开户
  • 现在做网站还用dw做模板了吗/免费下优化大师
  • 永康市网站建设/湖南长沙seo教育
  • 网站建设公司ipo/哈尔滨网络优化推广公司