63

How can I open a specific URL of the PlayStore/AppStore with flutter on Android and IOS, depending on which smartphone it is executed? I mean I want to open the application and not a browser or something like this.

In this thread I found some native way for android but how can I do this with flutter?

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
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)));
}

If there is currently no way to do this, it would be a nice feature to implement for the plugin url_launcher.

GreenTigerEye
  • 5,917
  • 9
  • 22
  • 33
  • Note that you can get your app's package name in pure Dart code, without writing any Java, by using the [flutter_android](https://pub.dartlang.org/packages/flutter_android) plugin's [`Context.packageName`](https://pub.dartlang.org/documentation/flutter_android/latest/android_content/Context/packageName.html) getter. – Arto Bendiken Nov 03 '18 at 07:36

7 Answers7

54

You can use url_luncher and open the url based on the platform like this:

import 'package:url_launcher/url_launcher.dart';

if (Platform.isAndroid || Platform.isIOS) {
  final appId = Platform.isAndroid ? 'YOUR_ANDROID_PACKAGE_ID' : 'YOUR_IOS_APP_ID';
  final url = Uri.parse(
    Platform.isAndroid
        ? "market://details?id=$appId"
        : "https://apps.apple.com/app/id$appId",
  );
  launchUrl(
    url,
    mode: LaunchMode.externalApplication,
  );
}

Note

  • You can get your iOS app id from http://itunes.apple.com/lookup?bundleId=YOUR_BUNDLE_ID then look for trackId
  • This wont work on iOS simulators as they don't have an app store
  • Setting the mode to LaunchMode.externalApplication will prevent the safari flash for a second
Iain Smith
  • 9,230
  • 4
  • 50
  • 61
  • please make an example for YOUR_BUNDLE_ID for 1 app on apple store . – Yogi Arif Widodo Jun 28 '22 at 11:43
  • YOUR_BUNDLE_ID = Bundle Identifier thats mean packange name ??? or how ??? ```{ "resultCount":0, "results": [] }``` – Yogi Arif Widodo Jun 28 '22 at 11:49
  • Here is an example with the apple pages app https://itunes.apple.com/lookup?bundleId=com.apple.Pages – Iain Smith Jun 28 '22 at 13:50
  • 1
    You will have set a bundle id for your iOS app so use that, it will be in your signing and capabilities section something like `com.companyname.appname` – Iain Smith Jun 28 '22 at 13:51
  • But its return file txt right ? Why not a json ? So in flutter we need to handle the download file and casting or read the inside file content right ? – Yogi Arif Widodo Jun 28 '22 at 19:08
  • ah my bad , we can pass content type and call it with `curl` to test , not direct by `browser access` , thankyou sir but i still didnt find the result , i only work with `http://itunes.apple.com/search?media=software&country=id&term=app%20name` i cant search my package name its just result resultCount 0. with `itunes.apple.com/lookup?bundleId=com.apple.Pages`, did you know why ? can you please real example ? – Yogi Arif Widodo Jun 28 '22 at 19:27
  • You don't need to handle this in Flutter, this is just to get your iOS app id, it is not dynamic. Once you have the value you can replace `YOUR_IOS_APP_ID` in the code example with the id you have retrieved – Iain Smith Jun 29 '22 at 11:56
53

You can use this Library,

Basically, To use this plugin, add launch_review as a dependency in your pubspec.yaml file.

launch_review: ^1.0.1

To use :

 import 'package:launch_review/launch_review.dart'; 

Then invoke the static launch method of LaunchReview anywhere in your Dart code. If no arguments are provided, it will consider the current package.

LaunchReview.launch();

To open the App Store page for any other applications, you can pass the app Id.

LaunchReview.launch(androidAppId: <package name>,
                iOSAppId: <ios app id>);
Naroju
  • 2,637
  • 4
  • 25
  • 44
10

This is like the most popular answer to this question, but without the need of saying the Android package. In addition, this solution doesn't show a toast on Android saying "Please Rate Application".

It depends on open_store and package_info_plus.

const appStoreId = "1234567890"; // Your app's App Store ID
final packageName = (await PackageInfo.fromPlatform()).packageName;

OpenStore.instance.open(
  androidAppBundleId: packageName,
  appStoreId: appStoreId,
);
Roc Boronat
  • 11,395
  • 5
  • 47
  • 59
9

You can do something similar in flutter:

import 'package:url_launcher/url_launcher.dart';

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);        
}

The exception/catch does not seem to work for some reason so adding 'finally' did the trick, finally :)

K Vij
  • 1,783
  • 1
  • 12
  • 19
  • 2
    You cannot catch an exception that occurs in a function/method returning `Future` unless you put `await` when you call it. – kaboc May 27 '20 at 05:29
  • ahhh I see, but how come the 'finally' part gets executed? – K Vij May 28 '20 at 01:24
  • 3
    The `finally` block is executed regardless of whether an exception occurred of not. I suspect that your code tries to open the store twice. – kaboc May 28 '20 at 10:19
  • hmm... i don't see that behavior... I'll test again. – K Vij May 28 '20 at 18:21
  • On flutter web, on Chrome Mobile the first method does not work, hence it opens the play store on the ugly web site. – MappaM Mar 22 '21 at 11:57
  • What about app store ? that is only for anrdoid, how to open app store – Kristi Jorgji Jul 28 '21 at 15:20
  • You don't need `try/catch`, You can use `if/else` with the `canLaunch()` method of `url_launcher` package. – Tayan Sep 05 '21 at 01:50
  • After using it for a while, I've realized that it opens the stock store (App Gallery) instead of Google Play on Huawei phones. To learn more, look here: https://developer.android.com/distribute/marketing-tools/linking-to-google-play#android-app – Tayan Sep 17 '21 at 16:32
9

You can use the url_launcher package for opening the appstore/playstore as follows :

      _launchURL(String url) async {
         if (await canLaunch(url)) {
             await launch(url);
         } 
         else {
             throw 'Could not launch $url';
         }
       }
developer43
  • 93
  • 1
  • 6
  • using this as a uri works "https://apps.apple.com/us/app/appname/id1485117463" but first safari shows up for a second or two then the safari itself redirects it to the actual app :D – Mehmet Filiz Jun 28 '20 at 19:48
4

You can use this plug in. here

Super simple. To use this pulg in, just write the package name (app id) on your dart code like this.

OpenAppstore.launch(androidAppId: "com.facebook.katana&hl=ko", iOSAppId: "284882215")
moonyoung
  • 41
  • 1
0

You can also try store_launcher

Example

StoreLauncher.openWithStore(appId);
heqingbao
  • 307
  • 3
  • 6
  • It doesn't support Sound Null Safety :·( In addition, Android package name could be automatically read from the app build itself... – Roc Boronat Mar 17 '22 at 11:54