iOS8 Touch ID api接口调用

简介: Touch ID简介Touch ID是苹果公司提供的一种将指纹用作密码的简便方式。只需轻触i主屏幕按钮,就能激活Touch ID传感器。主屏幕按钮周围的钢圈检测手指通知 Touch ID读取指纹。


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的真机设备才能编译通过。

导入

[objc]   view plain copy
  1. #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];  
     }];  
      
}


目录
相关文章
|
2月前
|
人工智能 Java API
Google Gemini API 接口调用方法
Google 最近发布的 Gemini 1.0 AI 模型通过其升级版,Gemini,标志着公司迄今为止最为强大和多功能的人工智能技术的突破。
|
29天前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之Secret Key(AccessKey Secret)和 API KEY(AccessKey ID)该如何申请
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
|
1月前
|
存储 JSON API
批量采集抖音商品详情数据:推荐你使用API(通过商品id取商品详情商品主图sku属性)
批量采集抖音商品详情,建议使用API接口。步骤包括:注册抖音开放平台获取App Key和Secret,调用商品详情API接口传入商品ID及相关参数,解析返回的JSON获取商品信息(如名称、价格、主图和SKU)。此外,接口列表提供商品搜索、销售量查询、历史价格、订单管理等多种功能。已封装的API接口地址:c0b.cc/R4rbK2,可测试并联系获取SDK文件。
40 1
|
1月前
|
API 开发工具
企业微信api接口调用-触发企业微信推送会话列表
企业微信api接口调用-触发企业微信推送会话列表
|
1月前
|
API 开发工具
企业微信api接口调用-通过手机号或微信好友添加客户
企业微信api接口调用-通过手机号或微信好友添加客户
|
1月前
|
API 开发工具
企业微信api接口调用-触发推送企业微信联系人列表
企业微信api接口调用-触发推送企业微信联系人列表
|
1月前
|
API 开发工具
企业微信api接口调用-触发推送企业微信微信好友
企业微信api接口调用-触发推送企业微信微信好友
|
1月前
|
API
企业微信api接口调用-企业微信好友收发消息
企业微信api接口调用-企业微信好友收发消息
|
1月前
|
DataWorks 监控 API
DataWorks产品使用合集之在调用API创建质量监控规则中的校验器的ID可以在哪里找到
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
2月前
|
数据采集 分布式计算 DataWorks
DataWorks产品使用合集之DataWorks数据地图中的数据发现相关api接口调用如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
36 0

热门文章

最新文章