How to intercept Android App Link into a WebView?

1.3k Views Asked by At

I'm trying to implement the following behavior into my Android App.

The app is a native app with some parts using WebView, in this WebView there is the possibility of using different payment methods.

If the user has the app of that bank installed I want to be able to open the corresponding app. In some cases, the website loaded into the WebView launch a specific intent:// that I can easily intercept and try to redirect the user to the app, but for the cases where the website use the new Android App link I'm not able to intercept them because they are normal https:// calls.

I tried to load the Android App link into Chrome and this way the app is opened.

My question at this point is how I can replicate the behavior of Chrome into my WebView??

I didn't find that much informations on this specific case besides the Android Documentation

Thanks for your help :)

2

There are 2 best solutions below

6
On

You can intercept the url click by using custom WebViewClient for the webview.

1)Set the custom webview client for the webview

2)Override the below method and return true (you can do this based on particular url also. )

shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

3)Override below method and handle the click and start the activity using either package name or playstore url with intent.

public void onLoadResource (WebView view, String url)

WebView mWebView; 



WebViewClient customWebClient = new WebViewClient(){
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
//This can be modified based on the url . URL can be retrieved using request.getUrl()
        return true;
    }
    
    @Override
    public void onLoadResource(WebView  view, String  url){
        if( url.equals("") ){
            // launch playstore activity
              final String appPackageName = "package name of the application"
              try {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                  } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                  }
        }
    }
}
mWebView.setWebViewClient(customWebClient);
0
On

In your WebViewClient override method shouldOverrideUrlLoading()

override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
                val url = request.url.toString()                  

                // start intent with URIs that contain all patterns that you need
                if (url.contains("needed-pattern") {
                    try {
                        ContextCompat.startActivity(
                            view.context,
                            Intent(Intent.ACTION_VIEW, Uri.parse(url)),
                            null
                        )
                    } catch (e: ActivityNotFoundException) {
                        Log.d("Clicked app is not installed!")
                    }

                    return true
                }


                return false
            }