IOS8 TouchID使用介绍

 

本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123

IOS8将指纹识别技术开放出来了。我们能够利用用户设置的touch ID来进行用户鉴权。

TouchID的API主要集成在LocalAuthentication.framework中。将改framework加入到工程中,并且需要iPhone5S和IOS8系统的支持,就能使用TouchID的API

了。

TouchID的API非常简单:

1、使用之前,需要先判断TouchID是否支持

 

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;  

2、调用TouchID识别接口

 

 

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;  

 

 

policy枚举,表示需要使用到的权限,目前只有一个Biometrics,意思指的就是TouchID

 

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  
  2.     /// Device owner was authenticated using a biometric method. Biometrics (Touch ID) is required.  
  3.     /// If Touch ID is not enabled, policy evaluation fails.  
  4.     LAPolicyDeviceOwnerAuthenticationWithBiometrics = kLAPolicyDeviceOwnerAuthenticationWithBiometrics  
  5. } NS_ENUM_AVAILABLE(10_10, 8_0);  

 

 

localizedReason

使用TouchID的理由,会出现在弹框中,提示用户。这个字段必须填写且不为nil或空值,否则会抛NSInvalidArgumentException异常,

 

 

(void(^)(BOOL success,NSError *error))


TouchID输入的结果回调。使用block方式。

 

示例代码:

 

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. LAContext *context = [[LAContext alloc] init];  
  2. NSError *contextError = nil;  
  3. NSString *localizedReasonString = @"Need Authorize";  
  4.   
  5. if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&contextError]) {  
  6.     [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics  
  7.             localizedReason:localizedReasonString  
  8.                       reply:^(BOOL success, NSError *error) {  
  9.                           NSString *title, *message;  
  10.                             
  11.                           if (success) {  
  12.                               title = @"Authorize Success";  
  13.                               message = nil;  
  14.                           } else {  
  15.                               title = @"Authorize Faied";  
  16.                               message = error.localizedFailureReason;  
  17.                           }  
  18.                             
  19.                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title  
  20.                                                                               message:message  
  21.                                                                              delegate:nil  
  22.                                                                     cancelButtonTitle:@"OK"  
  23.                                                                     otherButtonTitles:nil];  
  24.                           [alertView show];  
  25.                       }];  
  26. else {  
  27.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Context Error"  
  28.                                                         message:contextError.localizedFailureReason  
  29.                                                        delegate:nil  
  30.                                               cancelButtonTitle:@"OK"  
  31.                                               otherButtonTitles:nil];  
  32.     [alertView show];  

转载于:https://www.cnblogs.com/Camier-myNiuer/p/4021582.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值