先在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
网友评论