简单记录一下如何在自己的 APP 中调用系统 Touch ID
截图.PNG
注意:
需要导入 LocalAuthentication.framework 框架
和 #import <LocalAuthentication/LocalAuthentication.h> 头文件
代码:
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
LAContext *lacontext = [[LAContext alloc] init];
NSError *error;
// 判断设备是否支持 Touch ID, 是否开启Touch ID
if ([lacontext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
// localizedReason 不能为空
[lacontext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"输入指纹改变背景色" reply:^(BOOL success, NSError * _Nullable error) {
// 这里回调是在分线程中,修改 UI 要记得会主线程
NSLog(@"currentTheard:%@",[NSThread currentThread]);
if (success) {
dispatch_sync(dispatch_get_main_queue(), ^{
self.view.backgroundColor = [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1.0];
});
}
if (error) {
switch (error.code) {
case LAErrorAuthenticationFailed:
NSLog(@"指纹错误");
break;
case LAErrorUserCancel:
NSLog(@"点击取消");
break;
case LAErrorUserFallback:
NSLog(@"用户选择密码输入");
break;
case LAErrorSystemCancel:
NSLog(@"系统终止了验证");
break;
case LAErrorPasscodeNotSet:
NSLog(@"没有设置指纹密码");
break;
case LAErrorTouchIDNotAvailable:
NSLog(@"不支持Touch ID");
break;
case LAErrorTouchIDNotEnrolled:
NSLog(@"设备没有进行Touch ID 指纹注册");
break;
case LAErrorTouchIDLockout:
NSLog(@"用户指纹错误多次,Touch ID 被锁定");
break;
case LAErrorAppCancel:
NSLog(@"取消(如突然来电话等其他情况)");
break;
case LAErrorInvalidContext:
NSLog(@"创建的指纹对象失效");
break;
default:
break;
}
}
}];
}
}
网友评论