touchID 和 FaceID~1

公司项目里面要 TouchID 和 faceID 来解锁安全中心

FaceID

iPhoneX 提供的刷脸功能与之前的设备的TouchID类似,都是属于生物验证的范畴。苹果爸爸也是为了照顾开发者,这两个功能对应的API基本相同,对于之前支持TouchID的APP其实可以在不做任何修改的基础上兼容FaceID,只是在一些UI样式上需要修改。

biometryType
iOS11之后LAContext新增biometryType属性,调用时候可以根据这个属性来判断当前设备是使用FaceID还是TouchID,并据此做UI样式上的调整

 typedef NS_ENUM(NSInteger, LABiometryType)
 {
     /// The device does not support biometry.
     LABiometryTypeNone API_AVAILABLE(macos(10.13.2), ios(11.2)),
     LABiometryNone API_DEPRECATED_WITH_REPLACEMENT("LABiometryTypeNone", macos(10.13, 10.13.2), ios(11.0, 11.2)) = LABiometryTypeNone,
     
     /// The device supports Touch ID.
     LABiometryTypeTouchID,
     
     /// The device supports Face ID.
     LABiometryTypeFaceID API_UNAVAILABLE(macos),
 } API_AVAILABLE(macos(10.13.2), ios(11.0)) API_UNAVAILABLE(watchos, tvos);


 /// Indicates the type of the biometry supported by the device.
 ///
 /// @discussion  This property is set only when canEvaluatePolicy succeeds for a biometric policy.
 ///              The default value is LABiometryTypeNone.
 @property (nonatomic, readonly) LABiometryType biometryType API_AVAILABLE(macos(10.13.2), ios(11.0)) API_UNAVAILABLE(watchos, tvos);

2.NSFaceIDUsageDescription
使用FaceID需要在info.plist中增加NSFaceIDUsageDescription权限申请说明,这个跟定位、拍照等一样,如果不增加默认提示如下,虽然不会崩溃,但最好还是加上。

3.其他
FaceID的调用方法跟TouchID一样,都是先判断再调用,具体流程参照分割线后的TouchID部分。

4.注意
FaceID如果 不间断连续尝试 次数超过5次之后,会弹窗提示如下,同时不再执行reply:对应的block,这个需要注意

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
           localizedReason:myLocalizedReasonString
                     reply:^(BOOL success, NSError *error) {
     //!!!超出次数,提示弹窗后,这里的block不会执行!!!
 }];

 

 

TouchID

iPhone 5s之后苹果推出的TouchID功能绝对是登录验证的一大神器,自此之后各种APP在涉及到登录时如果不把这一方式加上,估计都不好意思说是做APP的。这就苦了我们众程序猿,在开发中免不了要遇上各种坑。

在次我将自己曾经趟过的一些坑罗列了下

调用前的判断

在调用TouchID验证弹窗前最好先判断一下设备是否支持TouchID

 

    //创建安全验证对象
    LAContext * con = [[LAContext alloc]init];
    NSError * error;
    //判断是否支持密码验证
    /**
     * LAPolicyDeviceOwnerAuthentication 可输入手机密码的验证方式
     * LAPolicyDeviceOwnerAuthenticationWithBiometrics 只有指纹的验证方式
     */
    BOOL can = [con canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];

这里有两种验证方式可选:
LAPolicyDeviceOwnerAuthenticationWithBiometrics iOS8.0以上支持,只有指纹验证功能
LAPolicyDeviceOwnerAuthentication iOS 9.0以上支持,包含指纹验证与输入密码的验证方式

调用TouchID

 

//初始化上下文对象
LAContext *context = [[LAContext alloc] init];
//localizedFallbackTitle=@“”,不会出现“输入密码”按钮
context.localizedFallbackTitle = @"";
//错误对象
NSError *error = nil;
NSString *result = @"验证信息";

//判断是否支持密码验证
/**
 *LAPolicyDeviceOwnerAuthentication 手机密码的验证方式
 *LAPolicyDeviceOwnerAuthenticationWithBiometrics 指纹的验证方式
 */
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
        if(error.code == LAErrorTouchIDLockout) {
            
            BOOL can = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error];
            if (can) {
                [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
                    
                }];
                
            }
            else{
                NSLog(@"调起账号密码页面失败!!!");
            }
        }
    }];
}

注意context.localizedFallbackTitle = @"";如果不设置空值,则AlertView弹窗默认会有“输入密码”的选项,但是在LAPolicyDeviceOwnerAuthenticationWithBiometrics模式下点击“输入密码”不会有反应;LAPolicyDeviceOwnerAuthentication模式下点击可以唤起输入手机密码页面,页面如下,其中除了“指纹”两字是你的app名称,其他都不可定制

 

后面文章会有error.code 的说明

最后,祝大家告别 Bug!!!!!!

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值