Flutter使用uni_links实现未安装APP时跳转Google Play商店
问题
我在Flutter项目中使用uni_links包,已经实现了点击链接唤起APP并跳转到指定页面的功能,现有代码如下:
try { final initialURI = await getInitialUri(); if (initialURI != null) { debugPrint("Initial URI received $initialURI"); if (!mounted) { return; } setState(() { _initialURI = initialURI; print("just checking$_initialURI"); }); if (_initialURI!.path == '/doctor') { pageController.jumpToPage( 1, ); _bottomNavCubit.updateIndex(1); pageController.page == 1; currentIndex = 1; } else if (_initialURI!.path == '/pricing') { Navigator.push( context, MaterialPageRoute( builder: (context) => SubscriptionScreen( subscriptionScreenArgs: SubscriptionScreenArgs( isFromInstantConsutation: false), )), ); NavigationUtil.pop(context); } } else { debugPrint("Null Initial URI received"); } } on PlatformException { debugPrint("Failed to receive initial uri"); } on FormatException catch (err) { if (!mounted) { return; } debugPrint('Malformed Initial URI received'); setState(() => _err = err); }
现在需要实现:当APP未安装时,用户点击该链接能直接跳转到Google Play商店的我的应用页面,请问该如何实现?
解决方案
注意:APP未安装时Flutter代码无法执行,因此该功能必须在链接的跳转逻辑层面(后端/域名配置)实现,而非Flutter端代码处理。以下是具体实现方式:
1. 利用Android App Links的Fallback机制
若已配置Android App Links,可在域名服务器的跳转规则中设置fallback逻辑:
- 系统会优先尝试唤起已安装的APP;
- 若唤起失败(APP未安装),自动跳转至Google Play应用详情页(格式:
https://play.google.com/store/apps/details?id=你的应用包名)。
配置要点:
- 确保域名已完成
assetlinks.json上传和AndroidManifest的App Links配置; - 在域名对应的跳转页面中添加规则,触发唤起失败时的重定向。
2. 自定义中间跳转页面(前端/后端实现)
未配置App Links时,可通过中间Web页面实现跳转逻辑:
- 用户点击的链接先指向你的Web页面;
- 页面通过JavaScript尝试唤起APP深度链接;
- 若指定时间内(如1.5秒)未成功唤起,则自动跳转到Google Play应用页。
示例JS代码(嵌入中间跳转页面):
// 替换为你的APP深度链接和Google Play链接 const appLink = "your_app_scheme://path"; const playStoreLink = "https://play.google.com/store/apps/details?id=com.your.app.package"; function tryOpenApp() { const startTime = Date.now(); const iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = appLink; document.body.appendChild(iframe); setTimeout(() => { document.body.removeChild(iframe); if (Date.now() - startTime < 2000) { window.location.href = playStoreLink; } }, 1500); } window.onload = tryOpenApp;
3. Flutter端无需额外修改
你现有uni_links代码仅负责APP已安装时的跳转逻辑,无需针对未安装场景调整——因为该场景下代码根本不会执行。
内容的提问来源于stack exchange,提问作者fawad
相关产品推荐
相关产品推荐

