iOS 集成FaceId和TouchID

现在的iPhone手机一共有三种解锁方式

1、FaceId      2、TouchID    3、老手机密码解锁

我们都知道现在的支持FaceId的手机一共有四种,X、XR、XS、XSMax。其他是TouchID和密码解锁。所以在使用FaceId的时候我们需要判断是否支持。所以我们先判断是否支持

首先、我们先导入一个官方库

#import <SystemConfiguration/CaptiveNetwork.h>

然后我们导入下面的方法


/**
 判断手机是否是异形屏幕
 
 @return YES 是 异形屏幕
 */
+ (BOOL)iphoneIsProfiledScreen {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString*platform = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    if([platform isEqualToString:@"iPhone10,3"] || [platform isEqualToString:@"iPhone10,6"]  || [platform isEqualToString:@"iPhone11,8"] || [platform isEqualToString:@"iPhone11,2"] || [platform isEqualToString:@"iPhone11,6"] || [platform isEqualToString:@"iPhone11,4"] ) {
        return YES;
    } else {
        return NO;
    }
}

接下来我们自己写也可以,但是还要做好封装,不方便,所以我在网上找到了一个类,我们导入一个类,这个类是我在网上找到的,使用更方便,下面这个类的.h文件


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, LAContextErrorType) {
    LAContextErrorAuthorFailure   = 0,  // 指纹识别或FaceID识别错误
    LAContextErrorAuthorNotAccess = 1,  // 指纹识别或FaceID不支持等其他原因
};

typedef void(^LAContext_Success)(void);
typedef void(^LAContext_Failure)(NSError *tyError, LAContextErrorType feedType);

@interface LAContextManager : NSObject

+ (void)callLAContextManagerWithController:(UIViewController *)currentVC
                                   success:(LAContext_Success)success
                                   failure:(LAContext_Failure)failure;

@end

下面是.m文件


#import "LAContextManager.h"
#import <LocalAuthentication/LocalAuthentication.h>
@implementation LAContextManager

+ (void)callLAContextManagerWithController:(UIViewController *)currentVC
                                   success:(LAContext_Success)success
                                   failure:(LAContext_Failure)failure {
    if ( ![currentVC isKindOfClass:[UIViewController class]] ) {
        return;
    }
    NSString *message = [NSObject iphoneIsProfiledScreen] ? @"面容 ID 短时间内失败多次,需要验证手机密码" : @"请把你的手指放到Home键上";// 当deviceType为LAPolicyDeviceOwnerAuthentication的时候,iPhone X会需要前面这段描述
    NSInteger deviceType = LAPolicyDeviceOwnerAuthenticationWithBiometrics;//单纯指纹或FaceID,LAPolicyDeviceOwnerAuthentication会有密码验证
    LAContext *laContext = [[LAContext alloc] init];
    laContext.localizedFallbackTitle = @""; // 隐藏左边的按钮(默认是忘记密码的按钮)
    NSError *error = nil;
    BOOL isSupport = [laContext canEvaluatePolicy:(deviceType) error:&error];
    if ( isSupport ) {
        [laContext evaluatePolicy:(deviceType) localizedReason:message reply:^(BOOL s, NSError * _Nullable error) {
            if ( s ) {
                success();
            } else {
                failure(error, LAContextErrorAuthorFailure);
            }
        }];
    } else {
        NSLog(@" - - - -- - - -- - - - --%@",error);
        failure(error, LAContextErrorAuthorNotAccess);
    }
}


@end

这样我们就对这个识别进行简单的封装了、接下来就是使用,我们引入这个类

#import "LAContextManager.h"

下面是使用方法

[LAContextManager callLAContextManagerWithController:self success:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // @TODO success
                [NSObject showToastView:self.view WithMessage:@"打开成功"];
            });
        } failure:^(NSError *tyError, LAContextErrorType feedType) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // @TODO
                if (tyError.code == -8) {
                    // 超出TouchID尝试次数或FaceID尝试次数,已被锁
                    [NSObject showToastView:self.view WithMessage:@"超出TouchID尝试次数或FaceID尝试次数"];
                } else if (tyError.code == -7) {
                    // 未开启TouchID权限(没有可用的指纹)
                    [NSObject showToastView:self.view WithMessage:[NSObject iphoneIsProfiledScreen] ? @"iPhone 未设置FaceId" : @"未开启TouchID权限(没有可用的指纹)"];
                } else if (tyError.code == -6) {
                    if ( [NSObject iphoneIsProfiledScreen] ) {
                        // iPhoneX 设置里面没有开启FaceID权限
                        [NSObject showToastView:self.view WithMessage:@"iPhone 设置里面没有开启FaceID权限"];
                    } else {
                        // 非iPhoneX手机且该手机不支持TouchID(如iPhone5、iPhone4s)
                        [NSObject showToastView:self.view WithMessage:@"非iPhoneX手机且该手机不支持TouchID(如iPhone5、iPhone4s)"];
                    }
                } else {
                    // 其他error情况 如用户主动取消等
                    NSLog(@"其他error情况 如用户主动取消等========");
                    [NSObject showToastView:self.view WithMessage:@"其他error情况 如用户主动取消等========"];
                }
            });
        }];

 下面这个方法是类似与安卓的吐丝,一个黑色提示框

[NSObject showToastView:self.view WithMessage:@""];

//Toast提示框
+(void)showToastView:(UIView *)uiview WithMessage:(NSString *)message
{
    UIView *showview = [[UIView alloc]init];
    showview.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    UIWindow * window = [[UIApplication sharedApplication].windows lastObject];
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    label.numberOfLines = 0;
    [showview addSubview:label];
    showview.frame = CGRectMake((uiview.frame.size.width - LabelSize.width - 20)/2, uiview.frame.size.height - LabelSize.height-100, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:2.0 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}

这样就完成了所有设备的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值