3

I'm using WebView inside jetpack compose project. The project is about server-driven UI where one of component returns String as HTML. HTML inside contains <a href> tag, which on click should open URL on default browser or open.

To handle that event, I overrided the function shouldOverrideUrlLoading. The problem is when I click on that tag, it opens a blank page inside WebView. I captured the WebResourceRequest.url property inside and it returns about:blank#blocked. I tried to play a little bit with WebSettings inside WebView but it didn't help. Also tried to capture errors with possible ssl certificate problems, but didn't find any solution either.

Here is the code:

AndroidView(factory = {
            WebView(context).apply {
                with(this.settings){
                    this.allowContentAccess = true
                }
                webViewClient = object : WebViewClient() {
                    override fun shouldOverrideUrlLoading(
                        view: WebView?,
                        request: WebResourceRequest?
                    ): Boolean {
                        Log.d("url0", request?.url.toString()) //returns about:blank#blocked
                        return if (url != null && (url!!.startsWith("http://") || url!!.startsWith("https://"))) {
                            //open page in browser
                            true
                        } else if (url != null && (url!!.startsWith("mailto:"))) {
                            //handle email
                            true
                        } else {
                            false
                        }
                    }
loadDataWithBaseURL(null, "<a href=\\\"https://en.wikipedia.org/wiki/Log4j\\\">This is sample data</a>.", "text/html", "UTF-8", null)
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Ivan
  • 137
  • 1
  • 7
  • Maybe you have javascript disabled. Enabled your webView's javascript by using `webView.setJavascriptEnabled(true)` –  Dec 17 '21 at 11:19
  • @SagarBalyan thanks for the answer. Already tried but it's not working – Ivan Dec 17 '21 at 12:22
  • Does this answer your question? [WebView link click open default browser](https://stackoverflow.com/questions/4229494/webview-link-click-open-default-browser) – Johann Dec 17 '21 at 13:13

1 Answers1

0

After a while, I found that problem was in <a href> tag which I got as response from my endpoint:

<a href=\\\"https://en.wikipedia.org/wiki/Log4j\\\">

If you remove escape marks from String you got:

<a href=\"https://en.wikipedia.org/wiki/Log4j\">

which is not a valid format and that's why it showed a blank page.

Removing \ from String fixed my problem.

Ivan
  • 137
  • 1
  • 7