How to get Webview iframe link to launch the browser?

亡梦爱人 提交于 2019-12-20 10:40:58

问题


I'm using a WebView to display a page in which the html includes an iframe where src="xxxxx.php".

This iframe loads as an ad image with an underlying link. If I click on that image (link), it tries to load the new page within original iframe (which doesn't show much in that little space). What I want to happen is clicking on the link to open the referred page in a new browser window, leaving my app as is.

If I use the Android browser to display the original page and click on this iframe, it loads the link as a new page. How do I get the same behavior with a WebView? Using a WebViewClient with shouldOverrideUrlLoading() doesn't seem to be called by the iframe link.


回答1:


I had a similar issue with google ads in a WebView source, since they load in an iframe as well. This is how I resolved it:

Try this in your WebViewClient, typically under your shouldOverrideUrlLoading()

            @Override
            public void onLoadResource (WebView view, String url) {
                if (url.contains("googleads")) {
                    if(view.getHitTestResult().getType() > 0){
                        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                        view.stopLoading();
                        Log.i("RESLOAD", Uri.parse(url).toString());
                    }
                }
            }



回答2:


I can propose one fix to previous code:

@Override
    public void onLoadResource (WebView view, String url) {
        if (url.contains("googleads")) {
            if(view.getHitTestResult() != null && 
                    (view.getHitTestResult().getType() == HitTestResult.SRC_ANCHOR_TYPE ||
                    view.getHitTestResult().getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)){
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                view.stopLoading();
            }
        }
    }



回答3:


To detect links clicks inside of the iframe. The links should have additional parameters. I've found that:

  1. shouldOverrideUrlLoading of the WebViewClient will be executed for a case when iframe link has target parameter target ="_parent" or target="_top",
  2. onCreateWindow of the WebViewClient, in case, if the iframe link contains target="_blank" parameter

Seems the iframe links without target parameter not possible to track exactly via WebViewClient



来源:https://stackoverflow.com/questions/5641626/how-to-get-webview-iframe-link-to-launch-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!