iOS-iPhone X 验证Face ID

一、前言

iPhone X“全屏脸”“顶刘海”还是给iOS开发带来一定的工作量,UI的兼容是其一,另外有关于需要指纹的应用还要兼容一下Face ID

二、代码

废话不多说,直接上代码,Face ID的调用和验证分为两部分

1,判断是否设备支持Face ID

- (BOOL)checkFaceIDAvailable{

           LAContext *ctx = [[LAContext alloc] init];

            if (@available(iOS 11.0, *)) {

                    if ([ctx                canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics         error:NULL]){

                            if (ctx.biometryType == LABiometryTypeFaceID) {

                                return YES;

                            }else{

                                return NO;

                            }

                     }else{

                            return NO;

                     }

            } else {

                    NSLog(@"系统版本低于11.0");

                    return NO;

            }

}

这样就可以判断出,设备是否支持Face ID。

注:这个方法需要xcode9.0以上版本支持开发,否则会报错,LABiometryTypeFaceID是11.0以上版本支持的


2,调用Face ID

Face ID调用与指纹Touch ID函数是一样的,函数回调也一样。Face ID验证次数默认是5次!

-(void)startFaceIDVerify{

            if ([UIDevice currentDevice].systemVersion.floatValue < 11.0) {

                    NSLog(@"该系统低于11.0,不支持人脸验证");

                    return;

            }

            LAContext *ctx = [[LAContext alloc] init];

            if (@available(iOS 11.0, *)) {

                    if ([ctx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]) {

                    ctx.localizedFallbackTitle = @"再试一次";

                    [ctx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"使用FaceID进行登录验证" reply:^(BOOL success, NSError * _Nullable error) {

                            if (success) {

                                        NSLog(@"验证成功");

                            }else{

                                    switch (error.code) {

                                            case LAErrorUserFallback:

                                            {

                                                    NSLog(@"再试一次");      

                                            }

                                            break;

                                            case LAErrorUserCancel:

                                            {

                                                    NSLog(@"用户点击取消");

                                            }

                                            case LAErrorSystemCancel:{

                                                    NSLog(@"点击系统取消");

                                            }

                                            default:

                                            break;

                                      }

                                NSLog(@"fail");

                            }

                     }];    

                 } else {

                        NSLog(@"验证失败");

                  }

        } else {

                NSLog(@"该系统低于11.0,不支持人脸验证");

         }

}

三、总结

感谢各位的观看!!有意见请留言回复!

你可能感兴趣的:(iOS-iPhone X 验证Face ID)