由于最近的項目中用到了Touch ID竭望,這里就給大家介紹下用法
1)首先你需要導(dǎo)入LocalAuthentication.framework
,如下圖所示
2)在需要使用的文件導(dǎo)入#import <LocalAuthentication/LocalAuthentication.h>
3)因為TouchID只適用于iOS8及以上气忠,所以我們首先需要判斷系統(tǒng)是否為iOS8及以上SystemVersion [UIDevice currentDevice].systemVersion.doubleValue >= 8
然后判斷機(jī)型是否支持TouchID,判斷的方法為
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError *__autoreleasing *)error;
然后直接調(diào)用以下的方法直接調(diào)用TouchID
- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
[Demo示例][1]
[1]: https://github.com/lance017/TouchIDDemo
具體的代碼在這
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
#define SystemVersion [UIDevice currentDevice].systemVersion.doubleValue
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc]init];
button.center = self.view.center;
button.bounds = CGRectMake(0, 0, 100, 40);
[button setTitle:@"指紋識別" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)click {
//只有iOS8以上支持Touch ID
if (SystemVersion >= 8.0) {
LAContext *context = [[LAContext alloc]init];
NSError *error = nil;
//驗證機(jī)器是否支持指紋識別
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指紋識別
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"APP請求驗證" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"驗證成功");
}
switch (error.code) {
case LAErrorAppCancel:
NSLog(@"切換到其他APP第租,系統(tǒng)取消驗證");
break;
case LAErrorUserCancel:
NSLog(@"用戶取消驗證");
break;
case LAErrorUserFallback:
NSLog(@"用戶選擇輸入密碼膀息,切換主線程處理");
break;
default:
break;
}
if (error) {
NSLog(@"%@",error.localizedDescription);
}
}];
}else{
//機(jī)器不支持指紋識別
}
}else{
//iOS8以下不支持Touch ID
}
}
@end