解决webview内的iframe中的事件不可用的问题

来自:网络
时间:2020-10-14
阅读:
免费资源网 - https://freexyz.cn/

最近做Android的Webview开发,使用iframe中嵌入了很多页面,嵌入的页面却不可用,最后发现是

 webView.setWebViewClient(new WebViewClient() {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return super.shouldOverrideUrlLoading(view, url);
   }

不要覆写

shouldOverrideUrlLoading

这个方法,覆写这个方法会拦截ifame中的事件。

补充知识:Android 原生WebView访问使用iFrame网页问题(页面找不到了)

问题:

项目使用原生WebView访问使用了iFrame的网页出现的问题,列表页使用iFrame跳转到淘宝客的地址,单独访问淘宝客地址是能够打开。但列表页跳转过去总是提示“页面找不到了”,尝试很多方法,最终发现是WebView对第三方Cookie支持的问题。

解决:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);

完整配置:

  mWebView.setWebViewClient(new WebViewClient() {
   @Override
   public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
   }

   @Override
   public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed();
    super.onReceivedSslError(view, handler, error);
   }

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url);
   }

   @TargetApi(Build.VERSION_CODES.LOLLIPOP)
   @Override
   public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    return super.shouldInterceptRequest(view, request);
   }

   @Override
   public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    if (null != title && null != view && TextUtils.isEmpty(mTitle))
     title.setText(view.getTitle());
   }
  });
  mWebView.setWebChromeClient(new WebChromeClient() {
          @Override
          public void onProgressChanged(WebView webView, int i) {
           super.onProgressChanged(webView, i);
          }

          @Override
          public boolean onJsConfirm(WebView view, String url, String message, android.webkit.JsResult result) {
           return super.onJsConfirm(view, url, message, result);
          }

          @Override
          public void onShowCustomView(View view, CustomViewCallback callback) {
           super.onShowCustomView(view, callback);
          }

          @Override
          public void onHideCustomView() {
          }

          @Override
          public boolean onShowFileChooser(WebView webView, android.webkit.ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
           return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
          }

          @Override
          public boolean onJsAlert(WebView view, String url, String message, android.webkit.JsResult result) {
           return super.onJsAlert(view, url, message, result);
          }

          @Override
          public void onReceivedTitle(WebView view, String title) {
           super.onReceivedTitle(view, title);
          }
         }

  );

 WebSettings mWebSettings = mWebView.getSettings();

  mWebSettings.setAllowFileAccess(true);
  mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
  mWebSettings.setSupportZoom(true);
  mWebSettings.setBuiltInZoomControls(true);
  mWebSettings.setUseWideViewPort(true);
  mWebSettings.setSupportMultipleWindows(false);
  mWebSettings.setAppCacheEnabled(true);
  mWebSettings.setDomStorageEnabled(true);
  mWebSettings.setJavaScriptEnabled(true);
  mWebSettings.setGeolocationEnabled(true);
  mWebSettings.setAppCacheMaxSize(Long.MAX_VALUE);
  mWebSettings.setAppCachePath(getDir("appcache", 0).getPath());
  mWebSettings.setDatabasePath(getDir("databases", 0).getPath());
  mWebSettings.setGeolocationDatabasePath(getDir("geolocation", 0)
    .getPath());
  mWebSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
  mWebSettings.setLoadWithOverviewMode(true);
  mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  String mUserAgent = mWebSettings.getUserAgentString();
  mWebSettings.setUserAgentString(mUserAgent + " App/AppName");
  syncCookie();
  mWebSettings.setUseWideViewPort(true);
  mWebSettings.setLoadWithOverviewMode(true);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  } else {
   mWebSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
  }
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
   mWebSettings.setDisplayZoomControls(false);
  }
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   mWebSettings.setLoadsImagesAutomatically(true);
  } else {
   mWebSettings.setLoadsImagesAutomatically(false);
  }
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
  }
  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
   CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);

  mWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
  mWebView.setHorizontalScrollBarEnabled(false);
  mWebView.setHorizontalFadingEdgeEnabled(false);
  mWebView.setVerticalFadingEdgeEnabled(false);

  mWebView.requestFocus();
 private void syncCookie() {
  CookieSyncManager.createInstance(this);
  CookieManager cookieManager = CookieManager.getInstance();
  cookieManager.setAcceptCookie(true);
  CookieSyncManager.getInstance().sync();
 }

以上这篇解决webview内的iframe中的事件不可用的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持免费资源网。

免费资源网 - https://freexyz.cn/
返回顶部
顶部