硬件要求: iPhone 5s及以上
軟件要求: iOS 8.0 及以上
示例代碼:
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//指紋識(shí)別 硬件iPhone5S及以上 軟件iOS8及以上
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//創(chuàng)建本地認(rèn)證上下文
LAContext *context = [[LAContext alloc] init];
// LAPolicyDeviceOwnerAuthenticationWithBiometrics 指紋認(rèn)證
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) { //可以進(jìn)行指紋識(shí)別
//進(jìn)行指針識(shí)別 參數(shù)1: 驗(yàn)證類(lèi)型 參數(shù)2: 進(jìn)行識(shí)別的原因 參數(shù)3: 驗(yàn)證結(jié)果
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要通過(guò)指紋進(jìn)行支付" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"識(shí)別成功");
}else {
NSLog(@"識(shí)別失敗: %@", error);
switch (error.code) {
case -2:
NSLog(@"用戶(hù)取消");
break;
case -1: //3次就會(huì)停止指紋識(shí)別
NSLog(@"到達(dá)嘗試錯(cuò)誤次數(shù)");
break;
case -8: //5次就不再使用指紋識(shí)別
NSLog(@"取消指紋識(shí)別");
break;
default:
break;
}
}
}];
NSLog(@"可以進(jìn)行指紋識(shí)別");
}else {
NSLog(@"硬件沒(méi)有達(dá)到要求");
}
}
}
@end