Flutter中使用url_launcher无法通过亚马逊联盟链接唤起亚马逊App
解决步骤
1. 补全LSApplicationQueriesSchemes配置
亚马逊App支持的Scheme不止你添加的两个,把以下内容补充到info.plist的对应数组中:
<key>LSApplicationQueriesSchemes</key> <array> <string>itms-beta</string> <string>itms</string> <string>amzn</string> <string>amzn-music</string> <string>com.amazon.mobile.shopping</string> <string>com.amazon.mobile.android.shopping</string> </array>
2. 处理联盟短链的重定向问题
amzn.to属于短链,会先跳转到亚马逊网页链接,iOS不会自动触发App唤起。需要先解析短链的真实地址,再转换为亚马逊App可识别的Scheme格式:
- 通过HTTP请求获取短链重定向后的真实亚马逊链接
- 将网页链接(如
https://www.amazon.com/dp/XXXXXX)转换为amzn://dp/XXXXXX格式 - 优先用Scheme链接唤起App,失败则 fallback 到网页打开
修改后的示例代码:
Future _launchUniversalLinkIos(int index) async { final shortUrl = reversedList[index].acf!.link.toString(); try { // 获取短链重定向后的真实地址 final response = await http.head(Uri.parse(shortUrl), followRedirects: false); final realUrl = response.headers['location']; if (realUrl != null) { final uri = Uri.parse(realUrl); final pathSegments = uri.pathSegments; // 提取商品ID,构造App Scheme链接 if (pathSegments.contains('dp')) { final productId = pathSegments[pathSegments.indexOf('dp') + 1]; final appUrl = Uri.parse('amzn://dp/$productId'); // 先尝试唤起App,失败则打开网页 if (await canLaunchUrl(appUrl)) { await launchUrl(appUrl, mode: LaunchMode.externalApplication); } else { await launchUrl(Uri.parse(realUrl), mode: LaunchMode.externalApplication); } } else { // 非商品链接直接打开真实地址 await launchUrl(Uri.parse(realUrl), mode: LaunchMode.externalApplication); } } else { // 拿不到重定向地址就打开原短链 await launchUrl(Uri.parse(shortUrl), mode: LaunchMode.externalApplication); } } catch (e) { debugPrint('Launch url Error: $e'); } }
注意:需要在pubspec.yaml中添加http依赖:
dependencies: http: ^1.1.0
3. 检查Universal Link配置(可选)
如果你的域名配置了亚马逊的Universal Link关联,iOS会通过HTTPS链接直接唤起App,但需确保:
- 苹果开发者后台已为App配置关联域名
applinks:amazon.com - 亚马逊服务器能返回正确的
apple-app-site-association文件
内容的提问来源于stack exchange,提问作者Tdayo
相关产品推荐
相关产品推荐

