iOS 官方 Demo
https://developer.apple.com/library/ios/samplecode/KeychainTouchID/Introduction/Intro.html
https://segmentfault.com/a/1190000002516465
iOS系统的指纹识别功能最低支持的机型为iPhone 5s
,最低支持系统为iOS 8
,虽然安装iOS 7
系统的5s机型可以使用系统提供的指纹解锁功能,但由于API
并未开放,所以理论上第三方软件不可使用。
LocalAuthentication.framework
做iOS 8
以下版本适配时,务必进行API验证,避免调用相关API引起崩溃。
LAContext
指纹验证操作对象
- (void)authenticateUser { //初始化上下文对象 LAContext* context = [[LAContext alloc] init]; //错误对象 NSError* error = nil; NSString* result = @"Authentication is needed to access your notes."; //首先使用canEvaluatePolicy 判断设备支持状态 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { //支持指纹验证 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) { if (success) { //验证成功,主线程处理UI } else { NSLog(@"%@",error.localizedDescription); switch (error.code) { case LAErrorSystemCancel: { NSLog(@"Authentication was cancelled by the system"); //切换到其他APP,系统取消验证Touch ID break; } case LAErrorUserCancel: { NSLog(@"Authentication was cancelled by the user"); //用户取消验证Touch ID break; } case LAErrorUserFallback: { NSLog(@"User selected to enter custom password"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //用户选择输入密码,切换主线程处理 }]; break; } default: { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //其他情况,切换主线程处理 }]; break; } } } }]; } else { //不支持指纹识别,LOG出错误详情 switch (error.code) { case LAErrorTouchIDNotEnrolled: { NSLog(@"TouchID is not enrolled"); break; } case LAErrorPasscodeNotSet: { NSLog(@"A passcode has not been set"); break; } default: { NSLog(@"TouchID not available"); break; } } NSLog(@"%@",error.localizedDescription); } }
首先判断系统版本,iOS 8
及以上版本执行-(void)authenticateUser
方法,方法自动判断设备是否支持和开启Touch ID
。
感谢秋儿指出iOS 9加入了三种新的错误类型。
其中,LAErrorTouchIDLockout
是在8.0中也会出现的情况,但并未归为单独的错误类型,这个错误出现,源自用户多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁,这个错误的交互LocalAuthentication.framework
已经封装好了,不需要开发者关心。
LAErrorAppCancel
和LAErrorSystemCancel
相似,都是当前软件被挂起取消了授权,但是前者是用户不能控制的挂起,例如突然来了电话,电话应用进入前台,APP被挂起。后者是用户自己切到了别的应用,例如按home键挂起。
LAErrorInvalidContext
很好理解,就是授权过程中,LAContext对象被释放掉了,造成的授权失败。
// // ViewController.m // test_ touch_ID_01 // // Created by admin on 2/15/16. // Copyright © 2016 jeffasd. All rights reserved. // #import "ViewController.h" #import <LocalAuthentication/LocalAuthentication.h> @interface ViewController () @property(nonatomic, strong) UIButton *authority; @property(nonatomic, strong) UIButton *entryButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _authority = [UIButton buttonWithType:UIButtonTypeCustom]; _authority.frame = CGRectMake(100, 100, 200, 50); _authority.backgroundColor = [UIColor cyanColor]; [_authority setTitle:@"是否支持" forState:UIControlStateNormal]; [_authority addTarget:self action:@selector(canEvaluatePolicy) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_authority]; } #pragma mark - evaluatePolicy - (void)canEvaluatePolicy{ LAContext *context = [LAContext new]; __block NSString *message; NSError *error; BOOL success; success = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]; if (success) { message = [NSString stringWithFormat:@"Touch ID is available"]; }else{ message = [NSString stringWithFormat:@"Touch ID is not available"]; NSLog(@"%@", message); switch (error.code) { case LAErrorTouchIDNotEnrolled: NSLog(@"TouchID is not enrolled"); break; case LAErrorPasscodeNotSet: NSLog(@"A passcode has not been set"); break; default: NSLog(@"TouchID not available"); break; } NSLog(@"localized %@",error.localizedDescription); } NSLog(@"%@", message); } - (IBAction)entryButton:(UIButton *)sender { LAContext *context = [LAContext new]; __block NSString *message; // Set text for the localized fallback button. // context.localizedFallbackTitle = @"Enter Password 111"; // context.localizedFallbackTitle = @"111"; // context.localizedFallbackTitle = @""; //show the authentication UI with our reason string [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Unlock access to locked feature" reply:^(BOOL success, NSError * _Nullable authenticationError) { if (success) { //验证成功,主线程处理UI message = @"evaluatePolicy : success"; }else{ message = [NSString stringWithFormat:@"evaluatePolicy %@", authenticationError.localizedDescription]; NSLog(@"%@", message); NSLog(@"%@",authenticationError.localizedDescription); switch (authenticationError.code) { case LAErrorSystemCancel: { NSLog(@"Authentication was cancelled by the system"); //切换到其他APP,系统取消验证Touch ID break; } case LAErrorUserCancel: { NSLog(@"Authentication was cancelled by the user"); //用户取消验证Touch ID break; } case LAErrorUserFallback: { NSLog(@"User selected to enter custom password"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //用户选择输入密码,切换主线程处理 UIView *view = [UIView new]; view.frame = self.view.bounds; view.backgroundColor = [UIColor yellowColor]; [self.view addSubview:view]; }]; break; } case LAErrorAuthenticationFailed: { NSLog(@"User kLAErrorAuthenticationFailed"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //用户选择输入密码,切换主线程处理 }]; break; } default: { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //其他情况,切换主线程处理 }]; break; } } } NSLog(@"%@", message); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end