美文网首页
关于WebView采坑实录,以及解决方案

关于WebView采坑实录,以及解决方案

作者: 桑果63 | 来源:发表于2018-05-01 18:23 被阅读0次

    这边总结几个WebView的常见问题,以及相应的解决方法

    一、重定向问题

    问题描述:我们从网页A跳到网页B,但是跳转到B的过程其实经历了C页面,有C页面重定向到达的B,A->(C->B),这个时候想要从B返回A,使用webview的goBack方法,会直接到C,由C又会回到B,导致无法跳出。

    解决:问题的原因是因为我在webview的shouldOverrideUrlLoading里使用view.loadUrl(url),并且返回true导致。查阅官方文档,对于shouldOverrideUrlLoading的解释是这样的:

        /**
         * Give the host application a chance to take over the control when a new
         * url is about to be loaded in the current WebView. If WebViewClient is not
         * provided, by default WebView will ask Activity Manager to choose the
         * proper handler for the url. If WebViewClient is provided, return true
         * means the host application handles the url, while return false means the
         * current WebView handles the url.
         * This method is not called for requests using the POST "method".
         *
         * @param view The WebView that is initiating the callback.
         * @param url The url to be loaded.
         * @return True if the host application wants to leave the current WebView
         *         and handle the url itself, otherwise return false.
         * @deprecated Use {@link #shouldOverrideUrlLoading(WebView, WebResourceRequest)
         *             shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead.
         */
        @Deprecated
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    

    简单来说,return true 表示webview不会帮你处理内部跳转,而是根据你自己的代码实现(webView.loadUrl),反之,webview内部会帮你处理跳转;
    简单举个例子:这边的http://3g.cib.com.cn/home/00000.html会重定向到https://mobile.cib.com.cn/netbank/cn/index.html,打印跳转后的历史堆栈:

    • 使用webView.loadUrl,return true 方式
    WebHistoryItemChromium[ mOriginalUrl=http://3g.cib.com.cn/home/00000.html,  mUrl=http://3g.cib.com.cn/home/00000.html], WebHistoryItemChromium[ mOriginalUrl=https://mobile.cib.com.cn/netbank/cn/index.html,  mUrl=https://mobile.cib.com.cn/netbank/cn/index.html]]]
    
    • 使用默认处理 return false 方式
     WebHistoryItemChromium[ mOriginalUrl=http://3g.cib.com.cn/home/00000.html, mUrl=https://mobile.cib.com.cn/netbank/cn/index.html]]]
    

    可以看到,前者生成了2条记录,后者只有1条记录,这样使用webview.goBack就不会跳到重定向页面了。另外考虑到类似tel:xxx的url,webview无法识别的问题,需要对链接做一次过滤:

     @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (TextUtil.isEmpty(url)) {
          return true;
        }
        if (url.startsWith("http://") || url.startsWith("https://")) {
          return false;
        } else {
          //todo 自行处理
          return true;
        }
      }
    

    二、通过一个页面重复多次进行拦截返回的页面

    问题描述:例如https://3g.cib.com.cn/app/00282.html这个页面,webview在跳转到这个页面时,其实连续跳转了2次,这个导致返回的会到前面一个相同的页面,而之后又会相同的页面产生,导致返回不了。

    解决:这个问题解决其实很简单,webview跳转返回除了goBack之外还有一个goBackOrForward方法

        /**
         * Goes to the history item that is the number of steps away from
         * the current item. Steps is negative if backward and positive
         * if forward.
         *
         * @param steps the number of steps to take back or forward in the back
         *              forward list
         */
        public void goBackOrForward(int steps) {
            checkThread();
            mProvider.goBackOrForward(steps);
        }
    

    这个方法可以直接前进或者返回多级,于是我们只要在返回时检测历史队列中的相同url,直接跳过相同的url,返回2级就可以了goBackOrForward(-2)

    三、onPageFinished监听不到url跳转

    问题描述:某些情况下,webview在处理页面一些内部跳转时onPageFinished没有调用

    解决:在onLoadResource监听,onLoadResource是资源加载,只要页面资源变化,这个方法就会执行

    四、webview销毁

    问题描述:webview资源不销毁会引起内存泄露,相关部分可以查看更加具体的文章 链接

    解决

      if (webView != null) {
          //webView.removeJavascriptInterface("android"); //删除jsbridge
          webView.setWebChromeClient(null);
          webView.setWebViewClient(null);
          webView.getSettings().setJavaScriptEnabled(false);
          webView.clearCache(true);
          webView.removeAllViews();
    
          if (webView.getParent() instanceof ViewGroup) {
            ((ViewGroup) webView.getParent()).removeView(webView);
          }
          webView.destroy();
        }
    

    五、支持加载Https和Http混合模式

    问题描述:在Android5.0以下,默认是采用的MIXED_CONTENT_ALWAYS_ALLOW模式,即总是允许WebView同时加载Https和Http;而从Android5.0开始,默认用MIXED_CONTENT_NEVER_ALLOW模式,即总是不允许WebView同时加载Https和Http。

    解决

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          set.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    

    六、js对象注入漏洞

    问题描述:4.2以前,通过JavaScript,可以访问当前设备的SD卡上面的任何东西,甚至是联系人信息,短信等。可以看相关报道:乌云

    解决
    Android WebView 使用漏洞

    六、js 方法的混淆

    问题描述:webview与js交互的时候,需要使用自定义方法,这个方法名是不能被混淆的

    解决
    为了解决上述js漏洞问题,原则上所有定义的js方法都应该加上@JavascriptInterface ,而这些被加了@JavascriptInterface 的方法keep起来就好了

    -keepattributes *Annotation*  
    -keepattributes *JavascriptInterface*
    -keep public class org.mq.study.webview.DemoJavaScriptInterface{
        public <methods>;
    }
    

    相关文章

      网友评论

          本文标题:关于WebView采坑实录,以及解决方案

          本文链接:https://www.haomeiwen.com/subject/rwkfrftx.html