CoreBluetooth - TouchID应用

支持系统和机型:

  iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8

  虽然安装iOS 7系统的5s机型可以使用系统提供的指纹解锁功能,但由于API并未开放,所以理论上第三方软件不可使用。

依赖框架

LocalAuthentication.framework
#import <LocalAuthentication/LocalAuthentication.h>

注意事项

iOS 8以下版本适配时,务必进行API验证,避免调用相关API引起崩溃。

if(iOS8){xxx} // 系统版本验证

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])

使用类

LAContext 指纹验证操作对象

操作流程

  • 判断系统版本,iOS 8及以上版本执行-(void)authenticateUser方法,
  • 方法自动判断设备是否支持和开启Touch ID。

代码示例

  1 #import "ViewController.h"
  2 #import "MBProgressHUD+MJ.h"
  3 #import <LocalAuthentication/LocalAuthentication.h>
  4 
  5 #define iOS8 ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0)
  6 
  7 
  8 @interface ViewController ()
  9 - (IBAction)clickMethod:(id)sender;
 10 
 11 @end
 12 
 13 @implementation ViewController
 14 
 15 - (void)viewDidLoad {
 16     [super viewDidLoad];
 17     // Do any additional setup after loading the view, typically from a nib.
 18 }
 19 
 20 
 21 - (IBAction)clickMethod:(id)sender {
 22     
 23     if (!iOS8) { // iOS8以后支持 touchID
 24         // HUD 提示
 25         [MBProgressHUD showError:@"当前系统不支持touchID"];
 26         return;
 27     }
 28     
 29     // 调用touchID
 30     [self touchIDShow];
 31     
 32 }
 33 
 34 - (void)touchIDShow
 35 {
 36     // 创建指纹校验对象
 37     LAContext *context = [[LAContext alloc] init];
 38     
 39     NSError *error1 = nil;
 40     if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error1]) {
 41         // 弹出验证
 42         [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"touchID error" reply:^(BOOL success, NSError * _Nullable error) {
 43            
 44             if (success) { // 验证成功
 45                 NSLog(@"success");
 46             }else{
 47                 switch (error.code) {
 48                     case LAErrorSystemCancel:
 49                     {
 50                         NSLog(@"Authentication was cancelled by the system");
 51                         //切换到其他APP,系统取消验证Touch ID
 52                         break;
 53                     }
 54                     case LAErrorUserCancel:
 55                     {
 56                         NSLog(@"Authentication was cancelled by the user");
 57                         //用户取消验证Touch ID
 58                         break;
 59                     }
 60                     case LAErrorUserFallback:
 61                     {
 62                         NSLog(@"User selected to enter custom password");
 63                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 64                             //用户选择输入密码,切换主线程处理
 65                         }];
 66                         break;
 67                     }
 68                     default:
 69                     {
 70                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 71                             //其他情况,切换主线程处理
 72                         }];
 73                         break;
 74                     }
 75                 }
 76                  NSLog(@"\n%s\n ---line=%d\n -- error=%@\n", __FUNCTION__, __LINE__, error.localizedDescription);
 77             }
 78         }];
 79         
 80     }else{
 81         switch (error1.code) {
 82             case LAErrorTouchIDNotEnrolled:
 83                 NSLog(@"LAErrorTouchIDNotEnrolled");
 84                 break;
 85                 
 86                 
 87             case LAErrorPasscodeNotSet:
 88                 NSLog(@"LAErrorPasscodeNotSet"); // 此处触发showPasscodeResetAlert方法
 89                 break;
 90                 
 91             default:
 92                 NSLog(@"Touch ID is unaviliable");
 93                 break;
 94         }
 95     }
 96     /*
 97      typedef NS_ENUM(NSInteger, LAError)
 98      {
 99      //授权失败
100      LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
101      
102      //用户取消Touch ID授权
103      LAErrorUserCancel           = kLAErrorUserCancel,
104      
105      //用户选择输入密码
106      LAErrorUserFallback         = kLAErrorUserFallback,
107      
108      //系统取消授权(例如其他APP切入)
109      LAErrorSystemCancel         = kLAErrorSystemCancel,
110      
111      //系统未设置密码
112      LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,
113      
114      //设备Touch ID不可用,例如未打开
115      LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
116      
117      //设备Touch ID不可用,用户未录入
118      LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,
119      } NS_ENUM_AVAILABLE(10_10, 8_0);
120      
121      */
122     
123     [self dealWithError:error1];
124 }
125 
126 
127 - (void)dealWithError:(NSError *)error
128 {
129     if (error) {
130         NSLog(@"error = %@", error.localizedDescription);
131     }
132 }
133 @end

 

posted @ 2016-02-18 12:10  guangleijia  阅读(503)  评论(1编辑  收藏  举报