繁体   English   中英

iframe未在webview android中加载

[英]Iframe not loading in webview android

我有一个网络视图。 一切正常,但是当我打开具有 iframe 的页面时,iframe 不可见。 是否需要任何特定设置?

首先添加硬件加速并将以下几行添加到您的 webView

webView.setWebChromeClient(new WebChromeClient());

编辑 1

很难确定问题,但尝试添加WebChromeClientWebViewClient并且在加载这样的URL之前不要忘记启用 javascript

webView= (WebView) findViewById(R.id.webview); 
webView.setWebChromeClient(new WebChromeClient()); 
webView.setWebViewClient(new WebViewClient()); 
webView.getSettings().setJavaScriptEnabled(true);

编辑 2

如果您确实需要加载 iframe 的内容,请尝试使用Jsoup加载 html 页面,但您必须手动找到 iframe 并获取内容。

Document doc=...
Element iframe1 = doc.select("#iframe1").first();

if (iframe1!=null) {
   Document iframeContent = Jsoup.connect(iframe1.attr("src")).get();

  }

其中iframe1是您的iframe id 。

这可能是一个为时已晚的答案! 我希望它会帮助某人。

只需尝试为您的 webview 设置一个桌面用户代理

String DESKTOP_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
webView.getSettings().setUserAgentString(DESKTOP_USER_AGENT);

你能检查一下你是否有

android:hardwareAccelerated="true"

在您的 AndroidManifest 文件中,在该活动的<application>标记中。

以下 hack 为我在 webview 中加载 iframe 工作。希望有人仍然会发现它有用

 String webContent="your data to be loaded in webview";

if(webContent.contains("iframe")){
                Matcher matcher = Pattern.compile("src=\"([^\"]+)\"").matcher(webContent);
                matcher.find();
                String src = matcher.group(1);
                webContent=src;

            try {
                URL myURL = new URL(src);
                webView.loadUrl(src);

            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }else {

            webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + webContent, "text/html", "UTF-8", null);}

    }
}

另外不要忘记在清单文件中添加 Internet 权限。

<uses-permission android:name="android.permission.INTERNET" />

如果修改 webview 配置并使用 loadData 函数加载 iframe,则某些 iframe 在 webview 中将无法运行。 所以对于解决方案,我已经实现了AdvancedWebView库。

https://github.com/delight-im/Android-AdvancedWebView

在此 webview 中使用 loadHtml(URL) 方法,您可以在参数中传递 URL,您将能够在 webview 中查看 iframe。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM