WebViewClient不调用shouldOverrideUrlLoading [英] WebViewClient not calling shouldOverrideUrlLoading

查看:1101
本文介绍了WebViewClient不调用shouldOverrideUrlLoading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单. 在应用程序中,我们要跟踪当前显示的URL.为此,我们使用WebViewClient中的shouldOverrideUrlLoading回调,方法是将URL保存到每次更新的类字段中.这是相关代码:

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

但是,至少有一种情况,回调永远不会触发,并且mCurrentUrl字段不会更新.

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

网址: https://m.pandora.net/es-es/products/bracelets/556000

最后更新的网址(单击产品时永远不会调用shouldOverrideUrlLoading): https://m.pandora.net/es-es/products/bracelets

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

我尝试过使用onPageStarted()之类的回调,但是URL也被过滤了,自从其受保护的代码以来,似乎没有可访问的上游地址.

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.

在阅读有关WebView的android文档时,我发现了这一点:

Reading android documentation about WebView I found this:

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

当请求资源并解析使用自定义URL方案的链接时,新的WebView会应用其他限制.例如,如果您实现诸如shouldOverrideUrlLoading()或shouldInterceptRequest()之类的回调,则WebView仅针对有效URL调用它们.

但是由于上面的url是通用的并且应该符合标准,因此仍然没有意义.

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

对此有任何替代方案或解决方案吗?

Any alternative or solution to this?

推荐答案

当您单击该网页上的产品时,它将使用JavaScript加载新内容并使用 HTML5历史记录API .

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.

来自上述MDN文章:

这将导致URL栏显示 http://mozilla.org/bar.html ,但不会导致浏览器加载bar.html甚至检查bar.html是否存在.

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.

这些有时称为单页应用程序.由于实际加载的页面没有变化,因此不会调用页面加载的WebView回调.

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

如果您确切地知道要拦截的HTTP请求类型,则可以使用为每个请求调用的shouldInterceptRequest回调.该Web应用程序很可能会从API加载一些数据,例如,当产品展示时,您便可以检测到.

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.

如果无法检测到这种情况,但是您可以控制Web应用程序,则可以使用

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.

如果您无法控制加载的页面,则仍然可以尝试观察使用历史记录API的时间,然后通过JS接口在Android应用程序中调用方法.我尝试使用上一个链接中描述的方法在Chrome中观察这些事件,但似乎工作正常.

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.

这篇关于WebViewClient不调用shouldOverrideUrlLoading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆