mark 官方文档
Framework:Local Authentication
#import <LocalAuthentication/LocalAuthentication.h>
#import <LocalAuthentication/LocalAuthentication.h>
代码:
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"请验证";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
error:&authError])
{
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error)
{
if (success) {
NSLog(@"User authenticated successfully, take appropriate action");
} else {
NSLog(@"User did not authenticate successfully, look at error and take appropriate action");
switch (error.code) {
case LAErrorUserFallback:
//指纹错误后点击输入密码
break;
case LAErrorAuthenticationFailed:
//连续三次指纹错误之后
break;
case LAErrorTouchIDLockout:
//连续五次指纹错误之后,TouchID锁定(ios11.0之后被弃用)
break;
default:
break;
}
}
}];
} else {
NSLog(@"Could not evaluate policy; look at authError and present an appropriate message to user");
}
LAContext会提供一个指纹验证的界面和功能。
关于验证形式的枚举有两种:
//第一种是指纹识别的验证,在需要输入密码的时候需要自己设计界面和功能;
LAPolicyDeviceOwnerAuthenticationWithBiometrics
//第二种是指纹识别的验证或密码的验证,但这个密码的验证是设备的解锁密码,不一定是我们需要的(比如微信等的支付密码)
LAPolicyDeviceOwnerAuthentication