iOS8 Touch ID api接口调用

Touch ID简介

Touch ID是苹果公司提供的一种将指纹用作密码的简便方式。只需轻触i主屏幕按钮,就能激活Touch ID传感器。主屏幕按钮周围的钢圈检测手指通知 Touch ID读取指纹。Touch ID 不会储存指纹的任何图像。它只存储指纹的数学表达式。

Touch ID的指纹数据存储在A7处理器的一个叫"secure enclave"协处理器上,唯一的Touch ID指纹识别器与唯一单独的A7处理器匹配。也就是说你将Touch ID拆开装到别的iPhone 5s上,Touch ID是无法使用的,因为它无法读取到A7处理器上的指纹数据。


Touch ID接口

使用Touch ID需要导入LocalAuthentication.framework,必须在装有iOS8的真机设备才能编译通过。
导入
#import <LocalAuthentication/LAContext.h>
判断当前是否有可用的Touch ID
- (BOOL)canEvaluatePolicy
{
    LAContext *context = [[LAContext alloc] init];
    NSError *error;
    BOOL success;
    
    // test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled
    success = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    
    return success;
}

判断[context canEvaluatePolicy:error:];判断当前是否有可用Touch ID,设备没有设备没有TouchID或者TouchID未开启返回false,有TouchID并开启返回true.


调用显示验证界面

- (void)evaluatePolicy
{
    LAContext *context = [[LAContext alloc] init];
    __block  NSString *msg;
    
    // show the authentication UI with our reason string
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"UNLOCK_ACCESS_TO_LOCKED_FATURE", nil) reply:
     ^(BOOL success, NSError *authenticationError) {
         if (success) {
             msg =[NSString stringWithFormat:@"EVALUATE_POLICY_SUCCESS"];
         } else {
             msg = [NSString stringWithFormat:@"EVALUATE_POLICY_WITH_ERROR : %@",
                    authenticationError];
         }
         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:msg message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
         [alertView show];
     }];
    
}

调用[contextevaluatePolicy:localizedReason:reply]可以显示验证界面,验证完毕后有一个回调。苹果官方文档规定第二个参数localizedReason一定要(shoudbe)使用用户的当前语言呈现。


真机演示(演示指纹识别成功的例子)


在真机测试中,指纹识别3次错误会退出验证界面并输出错误信息;在验证界面点击“输入密码”,也会退出,并输出错误信息。估计这是iOS8 Beta的bug,正常应该是指纹识别失败或者点击“输入密码”要弹出数字键盘。正式发布时,官方应该会处理好这个问题。

测试用的代码片段
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UIButton* btn = [[UIButton alloc] init];
    [btn setTitle:@"push me!" forState:UIControlStateNormal];
    CGRect frame = CGRectMake(self.view.frame.size.width/2 - 60., self.view.frame.size.height/2, 120., 80.);
    [btn setFrame:frame];
    
    [btn addTarget:self action:@selector(showTouchId) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
}

- (void)showTouchId
{
    if ([self canEvaluatePolicy]) {
        [self evaluatePolicy];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"5s ok?" message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [alertView show];
    }
}

#pragma mark - Tests

- (BOOL)canEvaluatePolicy
{
    LAContext *context = [[LAContext alloc] init];
    NSError *error;
    BOOL success;
    
    // test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled
    success = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    
    return success;
}

- (void)evaluatePolicy
{
    LAContext *context = [[LAContext alloc] init];
    __block  NSString *msg;
    
    // show the authentication UI with our reason string
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"UNLOCK_ACCESS_TO_LOCKED_FATURE", nil) reply:
     ^(BOOL success, NSError *authenticationError) {
         if (success) {
             msg =[NSString stringWithFormat:@"EVALUATE_POLICY_SUCCESS"];
         } else {
             msg = [NSString stringWithFormat:@"EVALUATE_POLICY_WITH_ERROR : %@",
                    authenticationError];
         }
         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:msg message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
         [alertView show];
     }];
    
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值