iOS 指纹登录

#import 

系统必须是8.0以后
主要用到的类是LAContext
代码如下

- (IBAction)loginAction:(id)sender {
    //非8.0返回
    if(![self canLocalAuthentication])
        return;
    LAContext *context = [[LAContext alloc] init];
    context.localizedFallbackTitle = @"输入密码";
    NSError *error = nil;
    if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"通过Home键验证手机" reply:^(BOOL success, NSError * _Nullable error) {
            if(success){
                [self showMessageInMainThread:@"Touch ID 验证成功"];
            }else if(error){
                switch (error.code) {
                    case LAErrorAuthenticationFailed:
                        [self showMessageInMainThread:@"验证失败"];
                        break;
                    case LAErrorUserCancel:
                        [self showMessageInMainThread:@"用户取消"];
                        break;
                    case LAErrorSystemCancel:
                        [self showMessageInMainThread:@"系统取消"];
                        break;
                    default:
                        break;
                }
                
            }else{
                [self showMessageInMainThread:@"当前设备不支持TouchID"];

            }
        }];
    }
}
-(void)showMessageInMainThread:(NSString *)string
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%@",string);
    });
}


-(BOOL)canLocalAuthentication
{
    if(NSFoundationVersionNumber

参考https://www.jianshu.com/p/9990b0f48488

你可能感兴趣的:(iOS 指纹登录)