关于iPhone手机的指纹解锁和面容解锁

首先大家要弄清一件事,那就是指纹解锁和面容解锁两种方式是分开的,这点不用我们操心,因为苹果爸爸已经很好的给我们区分开了。
1.在iPhoneX 以下的设备都是指纹解锁 Touch ID(5s以上设备)
2.在iPhoneX 以上的设备都是面容解锁 Face ID (包含iPhoneX)
接下来,说点正事。就是不管事指纹还是面容对于我们来说都是一套代码,一个框架。这是我比较感到欣慰的。

框架用的是 #import
类用的是 LAContext
根据 LAContext 的一些属性和方法进行设置和调用来完成我们项目的需求!
LAContext* context = [[LAContext alloc] init];
context.localizedFallbackTitle = @"自定义按钮的文本";
context.localizedCancelTitle = @"取消按钮的文本";
context.localizedReason = @"写解锁原因";
/*
LAPolicyDeviceOwnerAuthentication //系统密码
LAPolicyDeviceOwnerAuthenticationWithBiometrics //自定义密码
*/

核心代码

//指纹解锁调用方法
-(void)FingerprintIdentification2{
    
    // 初始化上下文对象
    LAContext* context = [[LAContext alloc] init];
    NSError* error = nil;
    NSString* result = @"需要您的支付进行支付";
  //localizedReason:result  (解锁原因)
    context.localizedFallbackTitle = @"快捷支付";
    context.localizedCancelTitle = @"取消";
    NSLog(@"data before authentication = %@",[context evaluatedPolicyDomainState]);
    
    // 首先使用canEvaluatePolicy 判断设备支持状态
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {

        // 支持指纹验证
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
            if (success) {
                //验证成功,主线程处理UI
                NSLog(@"验证成功...");
                NSLog(@"data after authentication = %@",[context evaluatedPolicyDomainState]);
            }else{
                NSLog(@"%@",error.localizedDescription);
                switch (error.code) {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"Authentication was cancelled by the system");
                        //切换到其他APP,系统取消验证Touch ID
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"Authentication was cancelled by the user");
                        //用户取消验证Touch ID
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        NSLog(@"User selected to enter custom password");
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //用户选择输入密码,切换主线程处理
                        }];
                        break;
                    }
                    case LAErrorAuthenticationFailed:
                    {
                        NSLog(@"Authentication Failed");
                        break;
                    }
                    case LAErrorTouchIDLockout:
                    {
                        NSLog(@"TOUCH ID is locked out");
                        break;
                    }
                    case LAErrorAppCancel:
                    {
                        NSLog(@"app cancle the authentication");
                        break;
                    }
                    case LAErrorInvalidContext:
                    {
                        NSLog(@"context is invalidated");
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //其他情况,切换主线程处理
                        }];
                        break;
                    }
                }
            }
        }];
    }else {
        NSLog(@"%@",error.localizedDescription);
        //不支持指纹识别,LOG出错误详情
        switch (error.code) {
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"TouchID is not enrolled");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"A passcode has not been set");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }
    }
}

谢谢阅读!有不正确的,请您指出,加以改正!

你可能感兴趣的:(关于iPhone手机的指纹解锁和面容解锁)