欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-05 08:38:32
最近做android的webview开发,使用iframe中嵌入了很多页面,嵌入的页面却不可用,最后发现是 webview.setwebviewclient(new webviewclient() {...

最近做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中的事件不可用的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。