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是否支持

  1. - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;  

2、调用TouchID识别接口
  1. - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;  
policy枚举,表示需要使用到的权限,目前只有一个Biometrics,意思指的就是TouchID
  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方式。

示例代码:

  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