0

There is a WebView in my android app. It loads a webpage and user may click links to launch other apps by url scheme. Now I need to avoid this action.

For example, when user click a correct deeplink or applink in WeChat, WeChat will just say something like "sorry we can not launch xxx..." in its WebView, and user can open that page in browser to jump to the target app. I want to do the same thing.

I have seen this similar question how-can-i-disable-deeplinks in SO but there is no useful answer. And I have also tried to do something in shouldOverrideUrlLoading or shouldInterceptRequest but neither works. That means I can recognize the special url in shouldOverrideUrlLoading, but override its return to true or false can not stop launching app because the deeplink action is not an override in WebView.

Krahmal
  • 195
  • 13

2 Answers2

0

url.startsWith("intent://")

 public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("intent://")) {
            // This is a deeplink URL, return true to disable deeplinking
             Toast.makeText(requireActivity(), "sorry we can not launch: "+url, Toast.LENGTH_SHORT).show();
            return true;
        } else {
            // This is not a deeplink URL, let the WebView handle it
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
Ashfaque
  • 179
  • 4
0

Add something like this to your WebViewClient:

 /**
 * The shouldOverrideUrlLoading(WebView view, String url) method is deprecated in API 24 and
 * the shouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is added in API 24.
 * If you are targeting both older versions of Android and compiling with 24+, you need both methods.
 * If not, you can delete the deprecated one
 */
@SuppressWarnings("deprecation") // Needs SDK 24 to be removed
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return handleUrl(Uri.parse(url));
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    return handleUrl(request.getUrl());
}

private boolean handleUrl(Uri uri) {
    // This can become more complex to support other url schemes like ftp
    return !uri.getScheme().startsWith("http");
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • As far a I know, deeplink is not a redirect in webview. So how can shouldOverrideUrlLoading stop it? – Krahmal Feb 16 '23 at 07:12
  • A deeplink in a web page is usually URI with a special scheme that is either sent a document.location change request or an href in the web page. For example `call` will create a hyperlink which tries to open the phone app. How does it work - the phone app registers that it supports `tel` scheme and when the web view doesn't know how to handle `tel`. In order for you to go out, you need to explictly launch an activity from shouldOverrideUrlLoading() . I assume this happens somewhere in your code. If you use my suggestion, I suspect that code wouldn't be called. – Doron Yakovlev Golani Feb 16 '23 at 08:54