Disable Android WebView/WebViewClient Initiated favicon.ico Request

后端 未结 5 853
独厮守ぢ
独厮守ぢ 2021-01-11 16:27

How can I disable the Android WebView/WebViewClient from sending out a request for favicon.ico when I call WebView.loadUrl()? I can see the call being made while profiling r

5条回答
  •  迷失自我
    2021-01-11 17:06

    You can simply pass some dummy InputStream to WebResourceResponse as shown below.

    my_webview.webViewClient = object : WebViewClient() {
        override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
            if (request?.url?.toString()?.toLowerCase()?.endsWith("/favicon.ico") == true) {
                val inputStream = "".byteInputStream(Charset.defaultCharset())
                return WebResourceResponse("text", "UTF-8", inputStream)
            } else {
                return super.shouldInterceptRequest(view, request)
            }
        }
    }
    

提交回复
热议问题