IOS8 TouchID使用介紹

 

本文轉載至 http://blog.csdn.net/jinkaiouyang/article/details/35555123xcode

IOS8將指紋識別技術開放出來了。咱們可以利用用戶設置的touch ID來進行用戶鑑權。

TouchID的API主要集成在LocalAuthentication.framework中。將改framework加入到工程中,而且須要iPhone5S和IOS8系統的支持,就能使用TouchID的APIapp

了。oop

TouchID的API很是簡單:ui

一、使用以前,須要先判斷TouchID是否支持lua

 

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

二、調用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);  

 

 

localizedReasonspa

使用TouchID的理由,會出如今彈框中,提示用戶。這個字段必須填寫且不爲nil或空值,不然會拋NSInvalidArgumentException異常,

 

 

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


TouchID輸入的結果回調。使用block方式。

 

示例代碼:code

 

[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];  
相關文章
相關標籤/搜索