You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:01:11