2016-03-08 89 views
0

我嘗試從我的Web視圖中獲取與WKWebView的JavaScript消息。但沒有出現IOS控制檯中......WKWebView中的javascript橋樑不起作用

我實現這個代碼:

- (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); 
} 

和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> 

我不會從我的網頁得到任何日誌,當我點擊在div(警報不起作用,但測試 - >你好作品)

感謝支持

回答

2

第一個問題 - 出於某種原因,您將WKWebView設置代碼放入webView:didFailLoadWithError:方法中。所以,你應該在收到錯誤後才創建WKWebView。看起來很奇怪。我已將此代碼移至viewDidLoad

第二個問題 - 您需要將您的WKWebViewConfiguration實例傳遞給-[WKWebView initWithFrame:configuration:]。在你的代碼configuration是完全沒用的。

第三個問題 - 你的HTML代碼只取出下一行後,工作對我來說:

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

我不是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); 
} 
+0

它這一點。謝謝。我加載之前添加了配置,它的工作原理 – user3279327

+0

@ user3279327很樂意提供幫助。請接受答案。 –