iOS 面容解锁 LocalAuthentication

先在info.plist中添加权限申请描述

key: Privacy - Face ID Usage Description

//
//  ViewController.m
//  faceDemo
//
//  Created by paul on 2023/9/13.
//

#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()
@property (nonatomic, strong) UIView *faceAuthView;
@end

@implementation ViewController

//解锁前view
- (UIView *)faceAuthView {
    if(!_faceAuthView){
        UIView *authView = [[UIView alloc] initWithFrame:self.view.bounds];
        authView.backgroundColor = UIColor.whiteColor;
        UIButton *giveAuthBtn = [[UIButton alloc] init];
        giveAuthBtn.frame = CGRectMake(0, 0, 350, 50);
        [giveAuthBtn setTitle:@"点击进行面容id解锁" forState:UIControlStateNormal];
        [giveAuthBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        giveAuthBtn.backgroundColor = UIColor.clearColor;
        [giveAuthBtn addTarget:self action:@selector(giveFaceAuth) forControlEvents:UIControlEventTouchUpInside];
        giveAuthBtn.center = authView.center;
        [authView addSubview:giveAuthBtn];
        _faceAuthView = authView;
    }
    return _faceAuthView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view addSubview:self.faceAuthView];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.yellowColor;
}



//用户点击face解锁
- (void) giveFaceAuth {
    
    if ([self isFaceIDAvailable]) {
        LAContext *context = [[LAContext alloc] init];
        NSError *error = nil;
        NSString *reasonString = @"请验证您的面容以解锁应用";
        
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                    localizedReason:reasonString
                              reply:^(BOOL success, NSError *error) {
                if (success) {
                    // 用户已通过面容验证
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // 在主线程中执行需要进行面容解锁后的操作
                        [self.faceAuthView removeFromSuperview];
                    });
                } else {
                    // 用户未通过面容验证或取消了验证
                    NSLog(@"Face ID 验证失败: %@", error.localizedDescription);
                }
            }];
        } else {
            // 无法使用面容识别功能
            NSLog(@"Face ID 不可用: %@", error.localizedDescription);
        }
    }

}

#pragma mark - 是否支持面容
- (BOOL)isFaceIDAvailable {
    LAContext *context = [[LAContext alloc] init];
    NSError *error = nil;
    BOOL isFaceIDAvailable = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    
    if (isFaceIDAvailable) {
        // 设备支持面容识别
        return YES;
    } else {
        // 设备不支持面容识别或配置错误
        NSLog(@"Face ID 不可用: %@", error.localizedDescription);
        return NO;
    }
}

/**
 * 在 iOS 11 及以上版本,可以通过 context.biometryType 来获取具体的生物识别类型,分为 LABiometryTypeFaceID(面容识别)
 * 和 LABiometryTypeTouchID(指纹识别)。而在 iOS 11 以下的版本,只支持指纹识别。
 */
- (NSString *)biometricType {
    LAContext *context = [[LAContext alloc] init];
    NSError *error = nil;
    
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        if (@available(iOS 11.0, *)) {
            if (context.biometryType == LABiometryTypeFaceID) {
                // 设备支持面容识别
                return @"Face ID";
            } else if (context.biometryType == LABiometryTypeTouchID) {
                // 设备支持指纹识别
                return @"Touch ID";
            }
        } else {
            // iOS 11 以下的版本只支持指纹识别
            return @"Touch ID";
        }
    }
    
    // 设备不支持生物识别或配置错误
    NSLog(@"生物识别不可用: %@", error.localizedDescription);
    return nil;
}
@end

不想动手可以找轮子 别人轮github

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容