Search code examples
flutterandroid-intentwebview-flutter

Convert intent web url to Android Intent


I currently integrate a webview (webview_flutter: 2.0.4) in my application for a payment process. Unfortunately, at some point the following url is launched:

intent://payment#Intent;action=ch.twint.action.TWINT_PAYMENT;scheme=twint;S.code=18223;S.startingOrigin=EXTERNAL_WEB_BROWSER;S.browser_fallback_url=;end

This URL should launch the "twint" application (Swiss payment solution) Unfortunately it doesn't work so I'm trying to recreate the corresponding android intent (android_intent: 2.0.0) but I can't.

Can anyone help me or any other idea for a solution?

I also tried with the url_launcher extension but I don't think it's the right solution.


Solution

  • I don't know if you can execute native kotlin/java code when using flutter, but this can be solved (natively) by using the following code (taken from https://stackoverflow.com/a/35612860):

    override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
    
            if (url.startsWith("intent://")) {
                val intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                return if (intent.resolveActivity(context.packageManager) != null) {
                    context.startActivity(intent)
                    true
                } else {
                    // handle error, e.g. open playstore
                    true
                }
            }
            /* 
              Rest of override url handling
              ...
            */
        }
    

    On Android API 30+ intent.resolveActivitywill only detect the installed twint app if you have the following entry in your AndroidManifest.xml:

    <queries>
        <intent>
            <action android:name="ch.twint.action.TWINT_PAYMENT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="twint" />
        </intent>
    </queries>