tip:需用真机测试
(在我们日常支付,或者app打开解锁等等可能用到指纹),系统给我们提供了LocalAuthentication框架;直接使用LAContext对象即可。
接下来看下使用方法:
- (void)setLAContext {
LAContext *mycontext = [[LAContext alloc] init];
NSError *error = nil;
NSString *myLocalReason = @"验证指纹test";
if ([mycontext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[mycontext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalReason reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"解锁成功");
}else {
if (error.code == LAErrorUserFallback) {
NSLog(@"输入密码验证");
}else if (error.code == LAErrorUserCancel){
NSLog(@"用户取消");
}else if (error.code == LAErrorSystemCancel){
NSLog(@"系统取消 ");
}
}
}];
}else{
NSLog(@"手机不支持指纹或者当前手机没有指纹");
}
}
此处列举全部验证失败结果:
/// 验证失败(指纹错误等等)
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
/// 用户自行取消指纹验证
LAErrorUserCancel = kLAErrorUserCancel,
/// 在touchID对话框,用户点击了输入密码验证方式
LAErrorUserFallback = kLAErrorUserFallback,
/// 被系统取消(比如接到电话,按home键,锁屏等等)
LAErrorSystemCancel = kLAErrorSystemCancel,
/// 设备没有使用密码,无法使用touchID
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
/// 设备不支持touchID功能
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
/// 没有录入touchID,
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
/// 多次使用touchID失败,touchID被锁,需要用户输入密码
LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0) = kLAErrorTouchIDLockout,
/// 当前软件被挂起,(应用进入前台时突然来了电话等)
LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,
/// 授权过程中LAContext被释放。
LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext