WKWebView中的javascript桥不起作用

Tho*_*ont 1 javascript objective-c wkwebview

我尝试使用从Web视图中获取javascript消息WKWebView。但是IOS控制台内部什么也没有出现...

我实现以下代码:

- (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);
}
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

单击div时,我的页面没有任何日志(警报也不起作用,但测试-> hello起作用)

谢谢支持

Bor*_*kyi 5

第一个问题- 由于某种原因,您将WKWebView设置代码放入了webView:didFailLoadWithError:方法中。因此,您应该WKWebView仅在收到错误后创建。看起来很奇怪。我已将此代码移入viewDidLoad

第二个问题-您需要将WKWebViewConfiguration实例传递给-[WKWebView initWithFrame:configuration:]。在您的代码configuration中完全没有用。

第三个问题-您的HTML代码仅在删除下一行后才对我有用:

$('#test').text('hello');
Run Code Online (Sandbox Code Playgroud)

我不是HTML / JS方面的专家。但是我想您正在尝试使用JQuery而不在HTML中引用它。

结果代码如下:

- (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);
}
Run Code Online (Sandbox Code Playgroud)