iOS下本地验证的简单使用(LocalAuthentication)

本地验证

说到本地验证,不得不提密码验证,指纹验证,Face ID也就是刷脸验证.
借用苹果的一张图来说明下认证的工作流程.
这里写图片描述

认证对比资料(密码,指纹信息,FaceID信息等),被存储在一块安全的区域.通过操作系统把认证信息(是否成功,报错信息等)发送给咱们的App.

本地生物验证的使用

    // 1.首先导入本地验证框架
    #import <LocalAuthentication/LocalAuthentication.h>
    // 2.这是苹果文档里头的代码
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    NSString *myLocalizedReasonString = <#String explaining why app needs authentication#>;

    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                                reply:^(BOOL success, NSError *error) {
                if (success) {
                    // User authenticated successfully, take appropriate action
                } else {
                    // User did not authenticate successfully, look at error and take appropriate action
                }
            }];
    } else {
        // Could not evaluate policy; look at authError and present an appropriate message to user
    }
  1. 需要创建一个LocalAuthentication的上下文
  2. evaluatePolicy表明是验证方式,大家也知道Face ID刷脸是iPhone X独占.但是iPhone X并没有指纹识别.相当于本地验证非密码方式的就只有生物验证(对于iPhone X是Face ID,对于非iPhone X的设备是指纹识别,LAPolicyDeviceOwnerAuthenticationWithBiometrics).
    typedef NS_ENUM(NSInteger, LAPolicy)
    {
        LAPolicyDeviceOwnerAuthenticationWithBiometrics//生物验证
        LAPolicyDeviceOwnerAuthentication//密码验证
    }
  1. 这个canxxx方法,在使用AV框架调用相机的时候大家肯定都用过.调用闪光灯什么的,作用主要分两点,1.防止崩溃.2.是否可用.
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),//看到这个就知道Face ID上Mac无望了.
}

验证错误枚举的话都在LAError.h这个头文件中.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值