Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebViewClient not calling shouldOverrideUrlLoading

The problem is rather simple. In the application we want to keep track of the current url being displayed. For that we use shouldOverrideUrlLoading callback from the WebViewClient by saving the url into a class field for every update. Here is the relevant code:

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true); 
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            mCurrentUrl = url;

            // If we don't return false then any redirect (like redirecting to the mobile
            // version of the page) or any link click will open the web browser (like an
            // implicit intent).
            return false;
        }



        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            ...
        }
    });
    mWebView.loadUrl(mInitialUrl);

However, there is at least one scenario, where the callback never gets triggered and the mCurrentUrl field doesnt get updated.

The url: https://m.pandora.net/es-es/products/bracelets/556000

Last updated url (shouldOverrideUrlLoading never gets called when clicking the product): https://m.pandora.net/es-es/products/bracelets

I have tried with callbacks like onPageStarted(), but the url also gets filtered and there doesn't seem to be an accessible one upstream since its protected code.

Reading android documentation about WebView I found this:

https://developer.android.com/guide/webapps/migrating.html#URLs

The new WebView applies additional restrictions when requesting resources and resolving links that use a custom URL scheme. For example, if you implement callbacks such as shouldOverrideUrlLoading() or shouldInterceptRequest(), then WebView invokes them only for valid URLs.

But still doesnt make sense since the above url is generic and should meet the standard.

Any alternative or solution to this?

like image 211
SuppressWarnings Avatar asked Oct 11 '16 14:10

SuppressWarnings


4 Answers

When you click a product on that web page, it loads the new content in with JavaScript and updates the visible URL in the address bar using the HTML5 History APIs.

From the above MDN article:

This will cause the URL bar to display http://mozilla.org/bar.html, but won't cause the browser to load bar.html or even check that bar.html exists.

These are sometimes called single-page applications. Since the actual loaded page doesn’t change, the WebView callback for page loads isn’t called.

In case you know precisely what kind of HTTP request you want to intercept, you could use the shouldInterceptRequest callback that gets called for each request. It’s likely that the web application loads some data from an API, for example when a product is shown, which you could then detect.

If detecting this isn’t possible, but you’re in control of the web application, you could use the Android JavaScript interface to invoke methods within the Android application directly from the web page.

If you’re not in control of the loaded page, you could still try to inject a local JavaScript file into the web page and observe when the history APIs are used, then call methods in your Android application over the JS interface. I tried observing these events in Chrome with the method described in the previous link and it seems to work fine.

like image 62
Leo Nikkilä Avatar answered Nov 08 '22 15:11

Leo Nikkilä


Maybe this helps someone, although the signature in the question is correct, but Android Studio suggests the following method signature:

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

which then never called. It took me a while to notice that the right signature is:

public boolean shouldOverrideUrlLoading(WebView view, String url) {

Sorry if this not 100% fit the question, but I believe this may help someone in the same situation. It's not always easy to notice that the second parameter is different.

like image 43
Mike Keskinov Avatar answered Nov 08 '22 14:11

Mike Keskinov


For me the problem was below line -

mWebView.getSettings().setSupportMultipleWindows(true);

After removing it shouldOverrideUrlLoading was being called.

like image 7
Hrishikesh Kadam Avatar answered Nov 08 '22 14:11

Hrishikesh Kadam


Please omit mWebView.getSettings().setDomStorageEnabled(true);

Then again try, if a new url found then will invoke shouldOverrideUrl()

like image 8
Jijo Avatar answered Nov 08 '22 15:11

Jijo