iOS 极验图形验证基本使用

  • 需要极验官网进行注册获取
    Github: gt3-ios-objc

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #import <GT3Captcha/GT3Captcha.h>

    @property (nonatomic, strong) GT3CaptchaManager *manager;

    [self.manager registerCaptcha:^{
    NSLog(@"注册成功");
    }];

    //创建验证视图的实例, 并添加到父视图上
    GT3CaptchaButton *captchaButton = [[GT3CaptchaButton alloc] initWithFrame:CGRectMake(0, 0, 300, 44) captchaManager:self.manager];
    captchaButton.center = self.view.center;
    [self.view addSubview:captchaButton];
  • manager懒加载

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    - (GT3CaptchaManager *)manager {
    if (!_manager) {
    _manager = [[GT3CaptchaManager alloc] initWithAPI1:@"https://rainingway.com/mobile-geetest/register" API2:@"https://rainingway.com/mobile-geetest/ajax_validate" timeout:5.0];
    _manager.delegate = self;
    _manager.viewDelegate = self;
    _manager.statisticDelegate = self;

    }
    return _manager;
    }
  • 代理实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    <GT3CaptchaManagerDelegate,GT3CaptchaManagerViewDelegate,GT3CaptchaManagerStatisticDelegate>

    **
    * 验证错误处理
    * 抛出内部错误, 比如GTWebView等错误
    * @param manager 验证管理器
    * @param error 错误源
    */
    - (void)gtCaptcha:(GT3CaptchaManager *)manager errorHandler:(GT3Error *)error{
    NSLog(@"error = %@",error);
    }

    /**
    * 通知已经收到二次验证结果, 在此处理最终验证结果
    * 二次验证的错误只在这里返回, `decisionHandler`需要处理
    * @param manager 验证管理器
    * @param data 二次验证返回的数据
    * @param response 二次验证的响应
    * @param error 错误源
    * @param decisionHandler 更新验证结果的视图
    */
    - (void)gtCaptcha:(GT3CaptchaManager *)manager didReceiveSecondaryCaptchaData:(NSData *)data response:(NSURLResponse *)response error:(GT3Error *)error decisionHandler:(void (^)(GT3SecondaryCaptchaPolicy captchaPolicy))decisionHandler{
    if (!error) {
    //处理你的验证结果
    NSLog(@"\nsession ID: %@,\ndata: %@", [manager getCookieValue:@"msid"], [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    //成功请调用
    decisionHandler(GT3SecondaryCaptchaPolicyAllow);
    }
    else {
    //二次验证发生错误
    decisionHandler(GT3SecondaryCaptchaPolicyForbidden);
    NSLog(@"validate error: %ld, %@", (long)error.code, error.localizedDescription);
    }

    }

    /**
    * @abstract 用户主动关闭了验证码界面
    *
    * @param manager 验证管理器
    */
    - (void)gtCaptchaUserDidCloseGTView:(GT3CaptchaManager *)manager{
    NSLog(@"用户主动关闭了验证码界面");
    }

    /**
    * @abstract 通知接收到返回的验证交互结果
    *
    * @discussion 此方法仅仅是前端返回的初步结果, 并非验证最终结果。
    *
    * @param manager 验证管理器
    * @param code 验证交互结果, 0失败/1成功
    * @param result 二次验证数据
    * @param message 附带消息
    */
    - (void)gtCaptcha:(GT3CaptchaManager *)manager didReceiveCaptchaCode:(NSString *)code result:(NSDictionary *)result message:(NSString *)message{
    NSLog(@"通知接收到返回的验证交互结果captchaState = %ld",(long)manager.captchaState);
    }

    /**
    * @abstract 更新验证状态
    *
    * @param manager 验证管理器
    * @param state 验证状态
    * @param error 错误信息
    */
    - (void)gtCaptcha:(GT3CaptchaManager *)manager updateCaptchaStatus:(GT3CaptchaState)state error:(GT3Error *)error{
    NSLog(@"更新验证状态captchaState = %ld",(long)manager.captchaState);
    if(manager.captchaState==GT3CaptchaStateSuccess){
    [manager closeGTViewIfIsOpen];
    }
    }
本文总阅读量