0

In my WebView I'm setting a WebViewClient in order to evaluate some JavaScript when the page finishes loading:

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');" +
                                        "}" +
                                    "}" +
                                ");");
    }
}

I'm doing the same thing in iOS which produces expected results:

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

But no joy in android. Any ideas? Am I calling it at the wrong time? Should I be using WebChromeClient?

Chris Byatt
  • 3,689
  • 3
  • 34
  • 61

2 Answers2

0

Try using the Javascript Bridge:

How to call javascript from Android?

It looks like that's what you're needing.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
0

The issue here was a race condition between the webapp and the app. The methods were working properly.

Chris Byatt
  • 3,689
  • 3
  • 34
  • 61