苹果指纹识别SDK

2017-06-02  本文已影响115人  大猿媛
SDK:

LocalAuthentication.framework,是苹果在iOS8开始提供的指纹识别也就是TouchID的SDK,允许App对用户身份进行本地验证。最低支持设备和系统:iPhone 5s,iOS8

框架解析 LAContext、LAError

LAContext,用于完成指纹识别功能的类

typedef NS_ENUM(NSInteger, LAPolicy)
{
    //指纹识别
    LAPolicyDeviceOwnerAuthenticationWithBiometrics NS_ENUM_AVAILABLE(10_12_2, 8_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0) = kLAPolicyDeviceOwnerAuthenticationWithBiometrics,
    //指纹识别两次失败后 用输入密码验证
    LAPolicyDeviceOwnerAuthentication NS_ENUM_AVAILABLE(10_11, 9_0) = kLAPolicyDeviceOwnerAuthentication

} NS_ENUM_AVAILABLE(10_10, 8_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0);

常用属性

// 回退按钮标题
// 默认标题为“输入密码”,当设置成空字符串的时候,该按钮被隐藏
@property (nonatomic, nullable, copy) NSString *localizedFallbackTitle;

// 取消按钮标题
// 设置验证TouchID时弹出Alert的取消按钮的标题
@property (nonatomic, nullable, copy) NSString *localizedCancelTitle;

// 允许验证失败最大次数
@property (nonatomic, nullable) NSNumber *maxBiometryFailures;

@property (nonatomic, nullable, readonly) NSData *evaluatedPolicyDomainState;

// 指定TouchID验证的间隔,间隔期之内可以免验证读取Keychain数据
@property (nonatomic) NSTimeInterval touchIDAuthenticationAllowableReuseDuration;

核心方法

//  判断设备是否支持Touch ID
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));
//  验证身份的函数
- (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString *)localizedReason
                 reply:(void(^)(BOOL success, NSError * __nullable error))reply;

LAError:主要用于Touch ID验证身份失败后的一些错误处理。

typedef NS_ENUM(NSInteger, LAError)
{
    //  授权失败
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
    // 用户取消Touch ID授权
    LAErrorUserCancel = kLAErrorUserCancel,
    // 用户选择输入密码
    LAErrorUserFallback = kLAErrorUserFallback,
    // 系统取消授权(例如其他APP切入)
    LAErrorSystemCancel = kLAErrorSystemCancel,
    // 系统未设置密码
    LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
    // 设备Touch ID不可用,例如未打开
    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
    // 设备Touch ID不可用,用户未录入
    LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
    // 用户多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁,这个错误的交互LocalAuthentication.framework已封装过,不需要开发者处理
    LAErrorTouchIDLockout   NS_ENUM_AVAILABLE(10_11, 9_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0) = kLAErrorTouchIDLockout,
    // 与LAErrorSystemCancel相似,都是当前软件被挂起取消了授权,LAErrorAppCancel该错误是用户自己切到了别的应用,例如按Home键挂起。
    LAErrorAppCancel        NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,
    // 授权过程中,LAContext对象被释放掉了,造成的授权失败
    LAErrorInvalidContext   NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
} 
实现代码
LAContext  *authenticationContext= [[LAContext alloc]init];
    authenticationContext.localizedFallbackTitle = @"点,验证其他";
    NSError *error;
    //1:检查Touch ID 是否可用
    if ([authenticationContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        NSLog(@"touchId 可用");
        //2:执行认证策略
        [authenticationContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要验证您的指纹来确认您的身份信息" reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                NSLog(@"通过了Touch Id指纹验证");
            }else{
                NSLog(@"error===%@",error);
                switch (error.code) {
                    case LAErrorAuthenticationFailed:{//授权失败
                        NSLog(@"Authentication Failed");
                        break;
                    }
                    case LAErrorUserCancel:{// 用户取消Touch ID授权
                        NSLog(@"Authentication was cancelled by the user");
                        break;
                    }
                    case LAErrorUserFallback:{//用户选择输入密码
                        NSLog(@"User selected to enter custom password");
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            // TODO SOMETHING
                        }];
                        break;
                    }
                    case LAErrorSystemCancel:{//系统取消授权(例如其他APP切入)
                        NSLog(@"Authentication was cancelled by the system");
                        //切换到其他APP,系统取消验证Touch ID
                        break;
                    }
                    case LAErrorAppCancel:{//与LAErrorSystemCancel相似,都是当前软件被挂起取消了授权,LAErrorAppCancel该错误是用户自己切到了别的应用,例如按Home键挂起。
                        NSLog(@"app cancle the authentication");
                        break;
                    }
                    case LAErrorInvalidContext:{//授权过程中,LAContext对象被释放掉了,造成的授权失败
                        NSLog(@"context is invalidated");
                        break;
                    }
                    default:{
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //其他情况,切换主线程处理
                        }];
                        break;
                    }
                }
            }
        }];
    }else{
        //todo goto 输入密码页面
        NSLog(@"error====%@",error);
        NSLog(@"抱歉,touchId 不可用");
    }
上一篇下一篇

猜你喜欢

热点阅读