M Karimi
M Karimi

Reputation: 3493

Create a button to open app from flutter web

I would like to create an app in Flutter. The web version contains a button that should open version of android or IOS app according user platform if mobile version of app was installed (like an app install or open banner).

How should I detect is app installed in web flutter?

update:

I tried below code using import 'package:universal_html/html.dart' pakage:

window.location.href = (defaultTargetPlatform ==
                      TargetPlatform.android)
                  ? 'https://play.google.com/store/apps/details?id=com.amazon.mShop.android.shopping'
                  : 'https://apps.apple.com/us/app/amazon-shopping/id297606951'; 

But this just open the store. I'm looking for a solution to open app directly if it was installed.

Upvotes: 3

Views: 3165

Answers (1)

DEFL
DEFL

Reputation: 1052

If this is fine for you, you can use an URL launcher. This way it opens the App store or play store and the user can either download the App or open it.

For Example flutter has a package that does most of this work: https://github.com/Purus/launch_review

LaunchReview.launch(androidAppId: "yourpackagename", iOSAppId: "appid");

You just need to pass your package name and on ios your app ID

You could also use an URL Launcher:

https://pub.dev/packages/url_launcher

The code would be similar to this:

_launchURL(String url) async {
   if (await canLaunch(url)) {
       await launch(url);
      } 
      else {
        throw 'Could not launch $url';
      }
    }

URL Example

try {
  launch("market://details?id=" + appPackageName);
} on PlatformException catch(e) {
    launch("https://play.google.com/store/apps/details?id=" + appPackageName);        
} finally {
  launch("https://play.google.com/store/apps/details?id=" + appPackageName);        
}

Note this code needs to be adapted

Also see this tutorial for help: https://flutteragency.com/open-appstore-playstore-url-in-flutter/

Edit:

If you want to directly open another app you can use something like this:

https://pub.dev/packages/external_app_launcher/

flutter pub add external_app_launcher

The Code would look like this then:

 await LaunchApp.openApp(
       androidPackageName: 'net.pulsesecure.pulsesecure',
       iosUrlScheme: 'pulsesecure://',
       appStoreLink: 'itms-apps://itunes.apple.com/us/app/pulse secure/id945832041',// openStore: false
       );

                // Enter the package name of the App you want to open and for iOS add the URLscheme to the Info.plist file.
                // The `openStore` argument decides whether the app redirects to PlayStore or AppStore.
                // For testing purpose you can enter com.instagram.android

More infos regarding implementation and additional setup infos you can find here: https://pub.dev/packages/external_app_launcher in the Readme

Upvotes: 1

Related Questions