深度連結允許應用使用者使用 URI 啟動應用。此 URI 包含方案、主機和路徑,並開啟應用到特定螢幕。

通用連結是一種專用於 iOS 裝置的深度連結,它只使用 httphttps 協議。

要設定通用連結,您需要擁有一個網路域。作為臨時解決方案,可以考慮使用 Firebase HostingGitHub Pages

設定好深度連結後,您可以驗證它們。要了解更多資訊,請參閱 驗證深度連結

建立或修改 Flutter 應用

#

編寫一個可以處理傳入 URL 的 Flutter 應用。

此示例使用 go_router 包來處理路由。Flutter 團隊維護 go_router 包。它提供了一個簡單的 API 來處理複雜的路由場景。

  1. 要建立一個新應用,請輸入 flutter create <app-name>

    flutter create deeplink_cookbook
  2. 要將 go_router 包作為依賴項包含在內,請執行 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 目標。

  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"?-->
    
    <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 目標。

  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。

例如:假設團隊 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 食譜的原始碼。