Android WebView JavaScript未获得评估

时间:2014-08-13 13:38:47

标签: java javascript android ios webview

在我的WebView中,我设置了WebViewClient,以便在页面加载时评估一些JavaScript

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        view.loadUrl("javascript:registerObjectDetailsCallback(" +
                                    "function(details) {" +
                                        "if (details.length == 1) {" +
                                            "window.location.href = 'callback:' + escape(details[0]);" +
                                        "} else if (details.length > 1) {" +
                                            "alert('Error: callback set by registerObjectDetailsCallback was passed multiple selection');" +
                                        "}" +
                                    "}" +
                                ");");
    }
}

我在iOS中做了同样的事情,产生了预期的结果:

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [serverWebView stringByEvaluatingJavaScriptFromString:
     [NSString stringWithFormat:@"registerObjectDetailsCallback("
                                    "function(details) {"
                                        "if (details.length == 1) {"
                                            "window.location.href = 'callback:' + escape(details[0]);"
                                        "} else if (details.length > 1) {"
                                            "alert('Error: callback set by registerObjectDetailsCallback was passed multiple selection');"
                                        "}"
                                    "})"]];
}

但是在Android中没有快乐。有任何想法吗?我是在错误的时间打电话吗?我应该使用WebChromeClient吗?

2 个答案:

答案 0 :(得分:0)

尝试使用Javascript Bridge:

How to call javascript from Android?

看起来这就是你所需要的。

答案 1 :(得分:0)

这里的问题是webapp和应用程序之间的竞争条件。方法运作正常。