Android. webview基本配置以及SSLerror错误过滤

@SuppressLint("SetJavaScriptEnabled")
    fun setWebSettings(context: Context?, mWebView: WebView) {
        val webSettings = mWebView.settings
        //        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
        // 设置支持JavaScript
        webSettings.javaScriptEnabled = true
        webSettings.loadWithOverviewMode = true
        webSettings.defaultTextEncodingName = "utf-8"
        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        webSettings.useWideViewPort = true
        //        webSettings.setSupportZoom(true);
        webSettings.domStorageEnabled = true //打开DOM存储
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.pluginState = WebSettings.PluginState.ON
        webSettings.allowFileAccess = true
        webSettings.builtInZoomControls = true
        webSettings.setSupportZoom(false) // 设置不能支持缩放
        webSettings.setAppCacheEnabled(true) // 开启H5(APPCache)缓存功能
        //        webSettings.setBlockNetworkImage(true);        //不加载图片
        webSettings.databaseEnabled = true // 应用可以有数据库
        val ua = webSettings.userAgentString
//        webSettings.userAgentString = "$ua JINRAN/Android"
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            webSettings.safeBrowsingEnabled = false
        }
        // 使用了https头,而h5页面中使用了http头,解决的问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE // 根据cache-control决定是否从网络上取数据
        //提高渲染优先级
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH)
        val myWebViewClient = MyWebViewClient()
        mWebView.webViewClient = myWebViewClient
        mWebView.webChromeClient = WebChromeClient()
    }

}

// 过滤ssl错误
private class MyWebViewClient : WebViewClient() {
    override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
        handler!!.proceed()
    }

你可能感兴趣的:(Android. webview基本配置以及SSLerror错误过滤)