面容ID或指纹的启用

1.使用前记得在info.plist中增加NSFaceIDUsageDescription权限申请说明,否则无法使用FaceID。

2.引入

#import


一.原生方法

//原生方法启用指纹/面部ID

- (void)fingerVerification

{

    if ([UIDevicecurrentDevice].systemVersion.floatValue<8.0) {

       NSLog(@"ios8.0以后才支持指纹识别");

        return;

    }

   //IOS11之后如果支持faceId也是走同样的逻辑,faceId和TouchId只能选一个

   LAContext*context = [[LAContextalloc]init];

   NSError*error = nil;

//如果支持faceId或TouchId

    if ([contextcanEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricserror:&error]) {

       // LAPolicyDeviceOwnerAuthenticationWithBiometrics: 用TouchID/FaceID验证

       // LAPolicyDeviceOwnerAuthentication: 用TouchID/FaceID或密码验证, 默认是错误两次或锁定后, 弹出输入密码界面(本案例使用)

        [contextevaluatePolicy:LAPolicyDeviceOwnerAuthenticationlocalizedReason:@"请验证已有指纹"reply:^(BOOLsuccess,NSError*_Nullableerror) {

            if (success) {

               NSLog(@"验证通过");

            } else {

               NSLog(@"验证失败:%@",error.description);

               switch(error.code) {

                        caseLAErrorSystemCancel:

                    {

                       NSLog(@"系统取消授权,如其他APP切入");

                       //系统取消授权,如其他APP切入

                        break;

                    }

                        caseLAErrorUserCancel:

                    {

                       //用户点击取消验证Touch ID

                       NSLog(@"用户取消验证Touch ID");

                        break;

                    }

                        caseLAErrorAuthenticationFailed:

                    {

                       //授权失败

                       NSLog(@"授权失败");

                        break;

                    }

                        caseLAErrorPasscodeNotSet:

                    {

                       //系统未设置密码

                       NSLog(@"系统未设置密码");

                        break;

                    }

                        caseLAErrorBiometryNotAvailable:

                    {

                       //设备Touch ID不可用,例如未打开

                       NSLog(@"设备Touch ID不可用,例如未打开");

                        break;

                    }

                        caseLAErrorBiometryNotEnrolled:

                    {

                       //设备Touch ID不可用,用户未录入

                       NSLog(@"设备Touch ID不可用,用户未录入");

                        break;

                    }

                        caseLAErrorUserFallback:

                    {

                        [[NSOperationQueuemainQueue]addOperationWithBlock:^{

                           //用户点击选择输入密码,切换主线程处理

                           NSLog(@"用户选择输入密码,切换主线程处理");


                        }];

                        break;

                    }

                   default:

                    {

                        [[NSOperationQueuemainQueue]addOperationWithBlock:^{

                           //其他情况,切换主线程处理

                           NSLog(@"其他情况,切换主线程处理");

                        }];

                        break;

                    }

                }

            }

        }];

    } else {

       NSLog(@"不支持指纹识别");

       NSLog(@"error : %@",error.localizedDescription);

    }

}


二.用封装好的第三方类的方法

使用YZAuthID只需一个方法就能轻松实现指纹/面容ID的验证识别功能

下载地址:https://github.com/micyo202/YZAuthID

或使用CocoaPods方式:pod 'YZAuthID'

使用方法

//启用指纹/面部ID

- (void)authVerification {


   YZAuthID*authID = [[YZAuthIDalloc]init];


    [authIDyz_showAuthIDWithDescribe:nilblock:^(YZAuthIDStatestate,NSError*error) {


        if (state ==YZAuthIDStateNotSupport) {// 不支持TouchID/FaceID

           NSLog(@"对不起,当前设备不支持指纹/面容ID");

        } else if(state ==YZAuthIDStateFail) {// 认证失败

           NSLog(@"指纹/面容ID不正确,认证失败");

        } else if(state ==YZAuthIDStateTouchIDLockout) {  // 多次错误,已被锁定

           NSLog(@"多次错误,指纹/面容ID已被锁定,请到手机解锁界面输入密码");

        } else if (state ==YZAuthIDStateSuccess) {// TouchID/FaceID验证成功

           NSLog(@"认证成功!");

        }


    }];

}

你可能感兴趣的:(面容ID或指纹的启用)