Flutter 打开各大应用市场

810 阅读1分钟

我用了Getx,部分函数可以改变一下自己实现。

只看核心内容

///市场链接列表,不嫌麻烦可以把初始化搬到这里来,然后加个 final 关键字
List<String> _marketUrls = [];
 ///当前准备打开的应用商店索引。如果朋友onError,则index +1 ,知道所有失败,将用web链接打开应用宝,必成功
int openIndex = 0;
      
   ///初始化执行
_initData() {
    _marketUrls = [
      ///程序会依次打开这些应用商场,如果有安装的话,如果已经被打开则拦截后续的打开,根据市场需求排序吧
      "market://details?id=${systemService.packageName}", //不清楚,但通用
      "vivomarket://details?id=${systemService.packageName}", //vivo 专用
      "mimarket://details?id=${systemService.packageName}", //小米 专用
      "appmarket://details?id=${systemService.packageName}" //华为 专用
      "oppomarket://details?packagename=${systemService.packageName}", //oppo 专用
      "store://appgallery.huawei.com/app/detail?id=${systemService.packageName}", //鸿蒙 专用
      "tmast://appdetails?pname=${systemService.packageName}", //腾讯应用宝
      "mzl://details/${systemService.packageName}", //魅族 没验证
      "nuba://detail/${systemService.packageName}" //努比亚 没验证
    ];
    if (GetPlatform.isIOS) _showAlertDialog();
    update(["update_app"]);
}

适配Ios端

  void _showAlertDialog() {
    showCupertinoModalPopup<void>(
      context: Get.context!,
      barrierDismissible: false,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: const Text('发现更新'),
        content: const Text('打开 AppStore 更新应用'),
        actions: <CupertinoDialogAction>[
          if (sysVersion['updateStatus'] != 1)
            CupertinoDialogAction(
              isDestructiveAction: true,
              onPressed: () {
              ///如果强制更新不会显示这个按钮,所以点击是去首页
                Navigator.pop(context);
              },
              child: Text('暂不更新'),
            ),
          CupertinoDialogAction(
            onPressed: () {
                ///必成功
               launchUrl(Uri.parse("https://itunes.apple.com/cn/app/1380512641"));
            },
            child: const Text('打开AppStore'),
          ),
        ],
      ),
    );
  }

打开安卓商店函数

  void openAndroidStore() async {
    final int _maxIndex = _marketUrls.length;
    if (openIndex >= _maxIndex) return print("权限不足,请打开应用商店搜索「xxx」更新");
    launchUrl(Uri.parse(_marketUrls[openIndex])).onError((handleError, e) {
      openIndex++;
      openAndroidStore(); ///循环调用自己 直到成功或结束
      return false;
    });
  }