4

My webapp at some point during its loading makes a request to a url which is callback:// in order to trigger a function in my android app.

I'm trying to catch this request with shouldOverrideUrlLoading but it's not called.. any ideas?

Is it the non-standard url scheme causing this?

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

2 Answers2

9

This is what they say in the shouldOverrideUrlLoading reference

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".

What's more important is what they don't say. The method will be called only when the user taps a link inside the Webview. You shound like you are calling the loadUrl() method in the WebView with your code. That does not result in a call to shouldOverrideUrlLoading. So that's what you are seeing.

middlestump
  • 1,035
  • 8
  • 22
1

i had same issue. After a long search in google i found a solution in Google's Android Developer documents.

if you are using version 4.2+ of android you have to add a prefix to your urls. You can add it server side or you can add:

String convertedHtml = yourHtmlString.replace("<a href=\"#", "<a href=link:\"");

Than you can override and check your link:

webView.setWebViewClient(new WebViewClient() {
            // Override URL

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //Log.e("url:", url);
                if (url.startsWith("link:"))
                {
                 // do what you want 
                 return true;
                }
            }
}

I hope this will help you.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • So.. what if I wanted more than one url schemes? I have two different ones that do different things – Chris Byatt Aug 14 '14 at 15:06
  • sure that's a case too. if you need more than one url schemes you should try to figure it out with a server side solution. i searched a google developer link for your question but i couldn't find the relevant page. you can check it out from here http://developer.android.com/reference/android/webkit/WebView.html – savepopulation Aug 15 '14 at 05:46