Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

shouldOverrideUrlLoading does not seem to get called properly + appreciation post #279

Open
philippdormann opened this issue Oct 17, 2020 · 3 comments
Labels

Comments

@philippdormann
Copy link

First off, thanks so much for this project! 馃檹
Saved me lots of time - wish I knew about this before ^^

During implementation of your library, I found a small problem (might as well be on my side)

When registering a WebViewClient() like this, I would normally expect the shouldOverrideUrlLoading function to be called.

wv?.webViewClient = object : WebViewClient() {
    override fun onReceivedError(view: WebView?,request: WebResourceRequest?,error: WebResourceError?) {
        // 
    }
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        Log.d("LOGGER", "url-triggered")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (request != null) {
                val u = request.url.toString()
                if(u.contains("keyword")) {
                    // website should not load, custom handler
                    return true
                }
            }
        }
    }
    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)
    }
    return super.shouldOverrideUrlLoading(view, request)
}

Expected

  • when loading a new url (calling wv?.loadUrl()) the shouldOverrideUrlLoading function should be called
  • as a result
    • url-triggered should be logged
    • the url should not be loaded, because of the return true

Actual

  • url gets loaded
  • shouldOverrideUrlLoading is not called; noting is logged

Possibility to Avoid this / get the expected behaviour

I would not call this a clean approach, but it works 馃檮

  • override onPageStarted
  • check url
  • wv?.stopLoading()
  • wv?.onBackPressed()
@ocram ocram added the question label Oct 20, 2020
@ocram
Copy link
Contributor

ocram commented Oct 20, 2020

Thanks!

If you have added a restricted set of allowed hostnames, either via addPermittedHostname or via addPermittedHostnames, then that set is evaluated first. So if the hostname of your (next) URL is not in that list, the request is cancelled immediately, before your own handler is called.

Other than that, when you set up a custom WebViewClient, your own handler should be called as you expect.

But, I鈥檓 afraid you might have to override the variant of shouldOverrideUrlLoading with String url as its second parameter (yes, although it鈥檚 deprecated), not the one with WebResourceRequest request. Can you try that?

(If this works, this aspect should really be fixed or documented. Sorry for that!)

@ocram ocram closed this as completed Oct 20, 2020
@ocram ocram reopened this Oct 20, 2020
@philippdormann
Copy link
Author

Thanks for your response @ocram !

Indeed, overriding shouldOverrideUrlLoading(view: WebView?, url: String?) works.
Would be awesome if you could include this in the docs (or even had a fix 馃檪)

Thanks again for your good work 馃憣
closing

@ocram
Copy link
Contributor

ocram commented Dec 16, 2020

Reopening until fixed.

Thanks again!

@ocram ocram reopened this Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants