跳转至正文

为 iOS 配置通用链接

了解如何为使用 Flutter 构建的 iOS 应用配置通用链接。

深度链接允许应用用户通过 URI 启动应用。该 URI 包含 scheme、host 和 path,可将应用打开到特定屏幕。

通用链接 是一种仅适用于 iOS 设备的深度链接类型,只使用 httphttps 协议。

要配置通用链接,你需要拥有自己的网站域名。作为临时方案,可考虑使用 Firebase HostingGitHub Pages

配置好深度链接后,你可以验证它们。了解更多,请参阅 验证深度链接

创建或修改 Flutter 应用

#

编写能处理传入 URL 的 Flutter 应用。

本示例使用 go_router package 处理路由。 Flutter 团队维护 go_router package。它提供简单的 API 来处理复杂的路由场景。

  1. 要创建新应用,输入 flutter create <app-name>

    flutter create deeplink_cookbook
    
  2. 要将 go_router package 添加为依赖,运行 flutter pub add

    flutter pub add go_router
    
  3. 要处理路由,在 main.dart 文件中创建 GoRouter 对象:

    main.dart
    dart
    import 'package:flutter/material.dart';
    import 'package:go_router/go_router.dart';
    
    void main() => runApp(MaterialApp.router(routerConfig: router));
    
    /// This handles '/' and '/details'.
    final router = GoRouter(
      routes: [
        GoRoute(
          path: '/',
          builder: (_, _) => Scaffold(
            appBar: AppBar(title: const Text('Home Screen')),
          ),
          routes: [
            GoRoute(
              path: 'details',
              builder: (_, _) => Scaffold(
                appBar: AppBar(title: const Text('Details Screen')),
              ),
            ),
          ],
        ),
      ],
    );
    

调整 iOS 构建设置

#
  1. 启动 Xcode。

  2. 打开 Flutter 项目 ios 文件夹内的 ios/Runner.xcworkspace 文件。

添加关联域

#
  1. 如有必要,启动 Xcode。

  2. 点击顶层的 Runner

  3. 在编辑器中,点击 Runner target。

  4. 点击 Signing & Capabilities

  5. 要添加新域,在 Signing & Capabilities 下点击 + Capability

  6. 点击 Associated Domains

    Xcode associated domains screenshot

  7. Associated Domains 部分,点击 +

  8. 输入 applinks:<web domain>。将 <web domain> 替换为你自己的域名。

    Xcode add associated domains screenshot

  1. 在你偏好的编辑器中打开 ios/Runner/Runner.entitlements XML 文件。

  2. <dict> 标签内添加关联域。

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
      <key>com.apple.developer.associated-domains</key>
      <array>
        <string>applinks:example.com</string>
      </array>
    </dict>
    </plist>
    
  3. 保存 ios/Runner/Runner.entitlements 文件。

要检查你创建的关联域是否可用,请执行以下步骤:

  1. 如有必要,启动 Xcode。

  2. 点击顶层的 Runner

  3. 在编辑器中,点击 Runner target。

  4. 点击 Signing & Capabilities。域应出现在 Associated Domains 部分。

    Xcode add associated domains screenshot

你已完成应用的深度链接配置。

将应用与网站域名关联

#

你需要在网站域名上托管 apple-app-site-association 文件。该文件告诉移动浏览器应打开哪个 iOS 应用,而不是浏览器。要创建该文件,请找到上一节创建的 Flutter 应用的 appID

定位 appID 的组成部分

#

Apple 将 appID 格式化为 <team id>.<bundle id>

  • 在 Xcode 项目中定位 bundle ID。

  • 开发者账户 中定位 team ID。

例如: 给定 team ID 为 S8QB4VV633, bundle ID 为 com.example.deeplinkCookbook,你应输入 appID 条目 S8QB4VV633.com.example.deeplinkCookbook

创建并托管 apple-app-site-association JSON 文件

#

该文件使用 JSON 格式。保存时不要包含 .json 文件扩展名。根据 Apple 文档,该文件应类似以下内容:

json
{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appIDs": [
          "S8QB4VV633.com.example.deeplinkCookbook"
        ],
        "paths": [
          "*"
        ],
        "components": [
          {
            "/": "/*"
          }
        ]
      }
    ]
  },
  "webcredentials": {
    "apps": [
      "S8QB4VV633.com.example.deeplinkCookbook"
    ]
  }
}
  1. appIDs 数组中的一个值设为 <team id>.<bundle id>

  2. paths 数组设为 ["*"]paths 数组指定允许的通用链接。使用星号 * 会将每个路径重定向到 Flutter 应用。如有需要,将 paths 数组值改为更适合你应用的设置。

  3. 在类似以下结构的 URL 托管该文件。

    <webdomain>/.well-known/apple-app-site-association

  4. 验证浏览器能否访问该文件。

#

使用实体 iOS 设备或模拟器测试通用链接。

  1. 测试前,在 iOS 设备或模拟器上安装 Flutter 应用,在目标设备上使用 flutter run

    Simulator screenshot

    完成后,Flutter 应用会显示在 iOS 设备或模拟器的主屏幕上。

  2. 如果使用模拟器测试,使用 Xcode CLI:

    xcrun simctl openurl booted https://<web domain>/details
    
  3. 如果使用实体 iOS 设备测试:

    1. 启动 备忘录 应用。

    2. 备忘录 应用中输入 URL。

    3. 点击生成的链接。

    如果成功,Flutter 应用会启动并显示其详情屏幕。

    Deeplinked Simulator screenshot

查找源代码

#

你可以在 GitHub 仓库中找到 deeplink_cookbook 示例 食谱的源代码。