iOS中的reCaptchav3实现

时间:2018-11-08 02:42:12

标签: ios objective-c recaptcha recaptcha-v3

我正在尝试在iOS中实现reCaptchav3,但无法使其正常工作。 reCaptchav2不可见可以正常工作。我不知道我的代码有什么问题。

HTML头:

<script src='https://www.google.com/recaptcha/api.js?render="SITE_KEY"'></script>
<script>
    grecaptcha.ready(function() {
      grecaptcha.execute('SITE_KEY', {action: 'OnSubmit'})
      .then(function(token) {
        // Verify the token on the server.
        document.getElementById("demo-form").submit();
      });
    });
</script>

按钮:

<div class="btn">
  <button class="g-recaptcha" data-callback="OnSubmit" data-expired-callback="OnExpired" data-error-callback="OnError" type="submit" disabled="">Button is disabled</button>
</div>

iOS:

- (void)initWebkitView {
    WKUserContentController *wkController = [[WKUserContentController alloc] init];

    [wkController addScriptMessageHandler:self name:@"reCaptchaiOS"];
    [wkController addUserScript:[self readScript]];

    WKWebViewConfiguration *wkConf = [[WKWebViewConfiguration alloc] init];
    [wkConf setUserContentController:wkController];

    wk = [[WKWebView alloc] initWithFrame:self.view.frame
                        configuration:wkConf];

    wk.backgroundColor = [UIColor clearColor];
    wk.opaque = NO;

    [wk setFrame:self.view.frame];
    [self.view addSubview:wk];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    NSArray<NSString*> *args = (NSArray*)message.body;

    if ([args[0] isEqualToString:@"OnSubmit"])
       [self captchaDidSubmitted:args[1]];
    else if ([args[0] isEqualToString:@"OnError"])
       [self captchaDidError:args[1]];
    else if ([args[0] isEqualToString:@"OnExpired"])
       [self captchaDidExpired:args[1]];
}

在iOS项目中添加了脚本:

function OnSubmit(response) {
    window.webkit.messageHandlers.reCaptchaiOS.postMessage(["OnSubmit", response]);
}

function OnError(response) {
    window.webkit.messageHandlers.reCaptchaiOS.postMessage(["OnError", response]);
}

function OnExpired(response) {
    window.webkit.messageHandlers.reCaptchaiOS.postMessage(["OnExpired", response]);
}

我对reCaptchav2使用了相同的代码,并遵循了googles指南,但没有得到答复。

0 个答案:

没有答案