美文网首页
简述Touch ID

简述Touch ID

作者: captain_Lu | 来源:发表于2016-08-02 18:15 被阅读62次

    Touch ID

    它是苹果公司在iOS 7中引入并在iPhone 5s上使用的新特性,是允许用户解锁设备和在App Store购物的生物识别技术.但当时并未对开发者开放,直至iOS 8,苹果才面向开发者开放TouchIDAPI,开发者能够增加TouchID作为一个应用程序的验证机制。

    Touch ID.png

    支持系统和机型

    集成指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8,虽然安装iOS 7系统的5s机型可以使用系统提供的指纹解锁功能,但由于API并未开放,所以理论上第三方软件不可使用。

    集成

    • 添加系统框架:
      LocalAuthentication.framework
    • 导入头文件
      #import<LocalAuthentication/LocalAuthentication.h>
    • 使用类:
      LAContext指纹验证操作对象
      LAContext *authenticationContext = [[LAContext alloc] init];
    • 两个重要方法:
    • 判断设备是否支持TouchID函数
      -(BOOL)canEvaluatePolicy:(LAPolicy)policyerror:(NSError*)error;
    • 验证身份函数
      -(void)evaluatePolicy:(LAPolicy)policylocalizedReason:(NSString *)localizedReasonreply:(void(^)(BOOLsuccess, NSError * error))reply;

    LAPolicy(评估策略)

    typedefNS_ENUM(NSInteger, LAPolicy)
    {
    n使用该设备的Touch ID验证
       LAPolicyDeviceOwnerAuthenticationWithBiometrics NS_ENUM_AVAILABLE(NA,8_0) = kLAPolicyDeviceOwnerAuthenticationWithBiometrics,
    n使用该设备的Touch ID和设备密码验证
        LAPolicyDeviceOwnerAuthenticationNS_ENUM_AVAILABLE(10_11, 9_0) = kLAPolicyDeviceOwnerAuthentication
    }NS_ENUM_AVAILABLE(10_10, 8_0);
    

    LAError

    • LAErrorAuthenticationFailed失败,用户没有成功的提供一个有效的认证资格
    • LAErrorUserCancel认证被用户取消
    • LAErrorUserFallback用户点击了 fallback 按钮(输入密码)
    • LAErrorSystemCancel认证被系统取消了(例如其他的应用程序到前台了)
    • LAErrorPasscodeNotSet认证不能开始,因为此台设备没有设置密码
    • LAErrorTouchIDNotAvailable认证不能开始,因为 touch id 在此台设备尚是无效的.
    • LAErrorTouchIDNotEnrolled认证不能开始,因为 touch id 没有录入指纹.
    • LAErrorTouchIDLockoutNS_ENUM_AVAILABLE(10_11, 9_0)指纹识别多次失败TouchID被锁
    • LAErrorAppCancelNS_ENUM_AVAILABLE(10_11, 9_0) 认证取消,退出app
    • LAErrorInvalidContextNS_ENUM_AVAILABLE(10_11, 9_0)认证是下文创建失败

    代码

    - (void)getAuthenticateUser
    {
        LAContext * authenticationContext = [[LAContext alloc] init];
        NSError * error = [[NSError alloc] init];
    
        if ([authenticationContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error])
        {
            [authenticationContext evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"需要您的指纹来确认您的身份信息" reply:^(BOOL success, NSError *error)
            {
                if (success)
                {
                    NSLog(@"恭喜,您通过了Touch ID指纹验证!");
                    _isLoad = YES;
                    [self getNodesData];
                }
                else
                {
                    NSLog(@"%@",error.localizedDescription);
                    switch (error.code) {
                        case LAErrorSystemCancel:
                        {
                            NSLog(@"Authentication was cancelled by the system");
                            //切换到其他APP,系统取消验证Touch ID
                            break;
                        }
                        case LAErrorUserCancel:
                        {
                            NSLog(@"Authentication was cancelled by the user");
                            //用户取消验证Touch ID
                            break;
                        }
                        case LAErrorUserFallback:
                        {
                            NSLog(@"User selected to enter custom password");
                            dispatch_async(dispatch_get_main_queue(), ^{
                                UIAlertView *inputPassWord = [[UIAlertView alloc]initWithTitle:@"请输入密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
                                [inputPassWord setAlertViewStyle:UIAlertViewStyleSecureTextInput];
                                [inputPassWord show];
                            });
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }];
        }
        else
        {
         //不支持指纹识别,LOG出错误详情
         switch (error.code) {
             case LAErrorTouchIDNotEnrolled:
             {
                 NSLog(@"TouchID is not enrolled");
                 break;
             }
             case LAErrorPasscodeNotSet:
             {
                 NSLog(@"A passcode has not been set");
                 break;
             }
             default:
             {
                 NSLog(@"TouchID not available");
                 break;
             }
         }
         NSLog(@"%@",error.localizedDescription);
       }
    }
    

    相关文章

      网友评论

          本文标题:简述Touch ID

          本文链接:https://www.haomeiwen.com/subject/tpzfsttx.html