简体   繁体   English

WKWebView中的javascript桥不起作用

[英]javascript bridge in WKWebView doesn't work

I try to get javascript message from my Web View with WKWebView . 我尝试使用WKWebView从Web视图获取javascript消息。 But nothing appear inside IOS console... 但是IOS控制台内部什么也没有出现...

I implement this code: 我实现以下代码:

- (void) webView:(WKWebView *)webView didFailLoadWithError:(NSError *)error {
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)];
    self.webView.backgroundColor = [UIColor whiteColor];

    self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    self.webView.scrollView.delegate = self;
    [self.view addSubview:self.webView];

    // Setup WKUserContentController instance for injecting user script

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]
         init];

    WKUserContentController* userController = [[WKUserContentController alloc]init];

    [userController addScriptMessageHandler: self name:@"notification"];
    configuration.userContentController = userController;

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/test.php"]]];
}

- (void)userContentController:(WKUserContentController*)userContentController
      didReceiveScriptMessage:(WKScriptMessage*)message {
    NSLog(@"%@", message.body);
}

And HTML/JS : 和HTML / JS:

<div id="test" onclick="test();">test</div>

<script type="text/javascript" src="jquery.js"></script>
<script>
function test (){
  alert('click');
  $('#test').text('hello');
  window.webkit.messageHandlers.notification.postMessage("click");
}
</script>

I don't get any log from my page when I click on the div (the alert doesn't work too but the test -> hello works) 单击div时,我的页面没有任何日志(警报也不起作用,但测试-> hello起作用)

Thanks for support 感谢你的支持

First issue - you placed your WKWebView setup code into webView:didFailLoadWithError: method for some reason. 第一个问题-由于某种原因,您将WKWebView安装代码放入了webView:didFailLoadWithError:方法中。 So, you are supposed to create WKWebView only after receiving error. 因此,您应该仅在收到错误后创建WKWebView Looks very strange. 看起来很奇怪。 I've moved this code into viewDidLoad . 我已经将此代码移到viewDidLoad

Second issue - you need to pass your WKWebViewConfiguration instance to -[WKWebView initWithFrame:configuration:] . 第二个问题-您需要将WKWebViewConfiguration实例传递给-[WKWebView initWithFrame:configuration:] In your code configuration is completely useless. 在您的代码configuration中完全没有用。

Third issue - your HTML code worked for me only after removing next line: 第三个问题-您的HTML代码仅在删除下一行后才对我有用:

$('#test').text('hello');

I'm not an expert in HTML/JS. 我不是HTML / JS方面的专家。 But I suppose that you're trying to use JQuery without referencing it in your HTML. 但是我想您正在尝试使用JQuery而不在HTML中引用它。

Resulting code looks like following: 结果代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKUserContentController* userController = [[WKUserContentController alloc] init];

    [userController addScriptMessageHandler:self name:@"notification"];
    configuration.userContentController = userController;

    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
    [self.view addSubview:self.webView];

    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [self.view addSubview:self.webView];

    NSString *string = @"<div id=\"test\" onclick=\"test();\">test</div>\n" \
    "<script type=\"text/javascript\" src=\"jquery.js\"></script>\n" \
    "<script>\n" \
    "function test (){\n" \
    "    alert('click');\n" \
    "    window.webkit.messageHandlers.notification.postMessage(\"click\");\n" \
    "}\n" \
    "</script>";

    [self.webView loadHTMLString:string baseURL:nil];
}

- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message
{
    NSLog(@"%@", message.body);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM