美文网首页
指纹支付验证

指纹支付验证

作者: 昵称nickName | 来源:发表于2018-03-29 11:35 被阅读0次

    工具类 OJPTools.m 中实现
    导入
    " #import <LocalAuthentication/LocalAuthentication.h> "

    //自己定义的返回值
    typedef NS_ENUM (NSInteger, OJPFingerprintVerify)   { 
        OJPFingerprintVerifyChanged = 0,    // 指纹变化
        OJPFingerprintVerifySuccess,        // 验证成功
        OJPFingerprintVerifyCancel,         // 取消验证
        OJPFingerprintVerifyFailed          // 验证失败
    };
    
    //指纹验证
    + (void) validFingerprintWithComplete:(void(^)(OJPFingerprintVerify verifyResault))complete {
        
        LAContext * context = [[LAContext alloc] init];
        
        NSData *domainState = [context evaluatedPolicyDomainState];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *oldDomainState = [defaults objectForKey:@"domainTouchID"];
        
        if (oldDomainState && ![oldDomainState isEqual:domainState]) {
            
            [OJPNSUserDefaultsTools setIsOpenFingerprint:NO];
            complete(OJPFingerprintVerifyChanged);
            return;
        }
        
        NSString *result = @"请验证已有的指纹,用于余额支付";
        NSError *error = nil;
        BOOL canUseTouchID = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error] ;
        if (canUseTouchID) {
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
                
                    if (success) {
                        complete(OJPFingerprintVerifySuccess);
                    } else if (error) {
                        
                        LAError errorCode = error.code;
                        switch (errorCode) {
                            case LAErrorUserCancel:// -2 在TouchID对话框中点击了取消按钮   0
                            case LAErrorSystemCancel:// -4 TouchID对话框被系统取消,例如按下Home或者电源键  0
                            case LAErrorAppCancel: //-9如突然来了电话,电话应用进入前台,APP被挂起啦"   0
                            case LAErrorNotInteractive:
                                complete(OJPFingerprintVerifyCancel);
                                break;
                                
                            case LAErrorAuthenticationFailed:// -1 连续三次指纹识别错误   1
                            case LAErrorUserFallback:// -3 在TouchID对话框中点击了输入密码按钮  1
                            case LAErrorInvalidContext: // -10LAContext传递给这个调用之前已经失效  1
                                complete(OJPFingerprintVerifyFailed);
                                break;
                            case LAErrorTouchIDLockout: //-8 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码  0
                            {//输入系统密码
                               [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
                            if (success) {
                                [self validFingerprintWithComplete:complete];
                            } else {
                                complete(OJPFingerprintVerifyCancel);
                            }
                        }];
                            }
                                break;
                            default:
                                complete(OJPFingerprintVerifyCancel);
                                break;
                        }
                    }
            }];
        }else { //输入系统密码
            
            switch (error.code) {
                case LAErrorPasscodeNotSet: //-5 设备系统未设置密码
                case LAErrorTouchIDNotEnrolled: // -7用户未录入指纹
                case kLAErrorTouchIDNotAvailable: // -6设备未设置Touch ID
                    complete(OJPFingerprintVerifyFailed);
                    break;
                default:
                {
                    [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
                            if (success) {
                                [self validFingerprintWithComplete:complete];
                            } else {
                                complete(OJPFingerprintVerifyCancel);
                            }
                        }];
                }
                    break;
            }
        }
    }
    

    //在设置中实现指纹支付开关

    - (void) switchAction:(UISwitch *)s1 {
        if (s1.on) {
            [self.viewController.view setUserInteractionEnabled:NO];
            [OJPTools validFingerprintWithComplete:^(OJPFingerprintVerify verifyResault) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.viewController.view setUserInteractionEnabled:YES];
                    switch (verifyResault) {
                        case OJPFingerprintVerifySuccess:
                        {
                            [OJPNSUserDefaultsTools setIsOpenFingerprint:YES]; //存本地
                            LAContext * context = [[LAContext alloc] init];
                            NSData *domainState = [context evaluatedPolicyDomainState];
                            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                            [defaults setObject:domainState forKey:@"domainTouchID"];
                            [defaults synchronize];
                        }
                            break;
                        default:
                        {
                            s1.on = NO;
                            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"指纹验证失败,未能开启指纹支付。" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            }];
                            [alertVC addAction:action];
                            [self.viewController presentViewController:alertVC animated:YES completion:nil];
                        }
                            break;
                    }
                });
            }];
            
        }else{
            [OJPNSUserDefaultsTools setIsOpenFingerprint:NO]; //存本地
        }
    }
    

    //支付代码

    if ([OJPNSUserDefaultsTools getIsOpenFingerprint]) {
            
            [self.view setUserInteractionEnabled:NO];
            [OJPTools validFingerprintWithComplete:^(OJPFingerprintVerify verifyResault) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.view setUserInteractionEnabled:YES];
                    
                    switch (verifyResault) {
                        case OJPFingerprintVerifySuccess:
                             //指纹支付接口
                            break;
                        case OJPFingerprintVerifyFailed:
                            [self payPasswordActoin];//密码支付
                            break;
                        case OJPFingerprintVerifyChanged:
                        {
                            [OJPNSUserDefaultsTools setIsOpenFingerprint:NO];//存本地
                            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"您的指纹库已发生改变,已为您关闭指纹支付,请使用密码支付或去支付设置中重新打开指纹支付。" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                //                        [defaults setObject:domainState forKey:@"domainTouchID"];
                                //                        [defaults synchronize];
                                [self payPasswordActoin];
                            }];
                            [alertVC addAction:action];
                            [self presentViewController:alertVC animated:YES completion:nil];
                        }
                            break;
                        default:
                            break;
                    }
                });
            }];
        }
        else {
            [self payPasswordActoin];//密码支付
        }
    }
    

    相关文章

      网友评论

          本文标题:指纹支付验证

          本文链接:https://www.haomeiwen.com/subject/rjlmcftx.html