Custom url scheme in Android webView

1.8k Views Asked by At

What is the current status, can you make an Android webView (not browser/Chrome Custom Tabs) interpret a link with a custom url scheme like "bankid:///?autostarttoken=xxxx-xxxxc&redirect=https://..."

Here the expected behavior is that the BankID-app should open when loading the link - Getting "err_unknown_url_scheme" instead. Is there any workaround for this? Or is the only way to use Intent?

Tried using Intent as below, which works, but then having troubles with the redirect parameter url, which would open up the browser which is not wanted. Replaced it with app id/null to stay in the webView/app, but then not getting the expected callback url in the webView.

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

        String url = request.getUrl().toString();
        if(!url.startsWith("http") && !url.startsWith("https")) {
            Uri newUri = replaceUriParameter(Uri.parse(url), 
            "redirect", BuildConfig.APPLICATION_ID);

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(newUri);

            startActivityForResult(intent, START_ACTIVITY_FOR_RESULT_LOGIN);
            view.reload();
            return true;
       }
}
1

There are 1 best solutions below

0
On

This might be a late answer but it might help others.

You were on the right direction for shouldOverrideUrlLoading but all you need is just using Intent on that it rest will be taken care of.

Here is the kotlin version of the solution.

override fun shouldOverrideUrlLoading(
    view: WebView?,
    request: WebResourceRequest
): Boolean {
   val url = request.url.toString()
   if (url.isNotEmpty()) {
       if (URLUtil.isNetworkUrl(url)) {
            return false
       } else if (url.startsWith("bankid")){
                   val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                  startActivity(intent)
             }
                     
   }
   return true
}