0

I'm building an app that must perform a registration inside a WebView. However, when I try to open the website URL inside the WebView, it tries to open a deeplink starting with market://. But I cannot let it happen and the registration must be done inside my WebView.

Is there anything I can do to prevent the deeplinks?

jeprubio
  • 17,312
  • 5
  • 45
  • 56
fcberg
  • 764
  • 13
  • 29

2 Answers2

0

You could try overriding shouldOverrideUrlLoading:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, 
                WebResourceRequest request) {
        val url = request.getUrl()
        if (url.startsWith("market")) {
            // Do something?
            return true
        }
        return false
    }
})

And return true when the current load has to be cancelled and false otherwise.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • I've already tried something like this. But when I implement shouldOverrideUrlLoading, the WebView looks to load an intermediary URL starting https://go.onelink.me/.... and stops loading and won't go to the next page. When I remove shouldOverrideUrlLoading, it works, but opens the deeplink. On the desktop web browser, everything works fine. – fcberg Jun 02 '20 at 11:11
  • It will stop loading when the shouldOverrideUrlLoading method returns true but it will continue loading the page if it returns false. You should return true only if its a market url – jeprubio Jun 02 '20 at 11:14
  • I know. I tried to just copy and paste your example. It also stops loading and won't go to the next page. – fcberg Jun 02 '20 at 11:16
  • OK, I forced the WebView to load next URL when return true. mWebView.loadUrl(url); But I'm not sure it will work for all situations. Maybe there are some query parameters missing or maybe there's some other locations inside the registration page... it's working, but I'm not sure it will work correct for all situations. – fcberg Jun 02 '20 at 11:33
  • I suppose you mean that you've forced it when returning false, because if you return true it means that the url loading should be overriden and the url should not be loaded. When you want the url to be loaded it should return false – jeprubio Jun 02 '20 at 11:35
  • Is there any way I can tell the web view to don't work like mobile? So their script won't identify that and won't try to open the deeplink. – fcberg Jun 02 '20 at 11:36
  • Well, you could hide the link in mobile with mediaqueries, but I don't think it's a good idea to rely on this. – jeprubio Jun 02 '20 at 11:38
-1

Try How to open desktop site in webview in android. The deep linking strategy will most likely not kick in when it sees that it's a desktop user agent and not a mobile one.

dvs
  • 1