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

Flutter iOS端URLLauncher深度链接异常:点击链接打开内置浏览器

iOS端Flutter聊天应用深度链接跳转修复方案

问题根源

你在info.plist的CFBundleURLSchemes字段里填了完整的HTTPS链接https://app.example.com是错误的——这个字段只能填自定义URL Scheme(比如examplechat),而HTTPS类型的深度链接属于Universal Links,需要单独配置。


1. 修正URL Scheme配置

把info.plist里的相关配置改成自定义scheme,示例:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>examplechat</string>
        </array>
    </dict>
</array>

之后点击examplechat://app.example.com/chat/xxx这类链接,iOS就能唤起你的应用。

2. 正确配置Universal Links(针对HTTPS链接)

如果要支持https://app.example.com这类链接直接跳转,需要完成以下两步:

(1)确认站点关联文件有效性

确保你的apple-app-site-association文件已正确托管在https://app.example.com/.well-known/路径下,文件内容要正确关联你的App Team ID和Bundle ID,示例:

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "你的TeamID.你的BundleID",
                "paths": ["*"]
            }
        ]
    }
}

(2)Xcode配置关联域名

  • 打开Xcode,进入项目的Signing & Capabilities页面,点击+ Capability,添加Associated Domains;
  • 在新增的Associated Domains里,添加applinks:app.example.com;
  • 也可以手动在info.plist中添加以下配置:
<key>NSUserActivityTypes</key>
<array>
    <string>NSUserActivityTypeBrowsingWeb</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>app.example.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

3. Flutter端处理跳转逻辑

用app_links插件监听深度链接,处理页面跳转:

(1)添加依赖

在pubspec.yaml中加入:

dependencies:
  app_links: ^3.4.0

(2)监听链接并跳转

import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';

void initDeepLinkListener(BuildContext context) async {
  final appLinks = AppLinks();

  // 监听前台打开的链接
  appLinks.uriLinkStream.listen((Uri uri) {
    handleDeepLink(uri, context);
  });

  // 处理应用启动时的初始链接
  final initialUri = await appLinks.getInitialUri();
  if (initialUri != null) {
    handleDeepLink(initialUri, context);
  }
}

void handleDeepLink(Uri uri, BuildContext context) {
  // 根据链接路径匹配会话页面
  if (uri.pathSegments.length >= 2 && uri.pathSegments[0] == 'chat') {
    final chatId = uri.pathSegments[1];
    // 跳转到对应会话页面
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ChatPage(chatId: chatId)),
    );
  }
}

4. 测试注意事项

  • 必须用真机测试,模拟器无法正确触发Universal Links;
  • 测试链接要通过短信、备忘录等第三方应用打开,直接在浏览器里点击可能无效;
  • 确保App已用正确的Team ID签名,和apple-app-site-association里的一致。

内容的提问来源于stack exchange,提问作者Nirmal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:43:12