跳到主內容

為 iOS 設定通用連結

瞭解如何為使用 Flutter 構建的 iOS 應用設定通用連結。

深度連結 (Deep linking) 允許應用使用者透過 URI 啟動應用。此 URI 包含方案 (scheme)、主機 (host) 和路徑 (path),並將應用開啟至特定螢幕。

通用連結 (Universal link) 是一種 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 檔案。

新增關聯域 (Associated Domains)

#
  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. 啟動 備忘錄 (Note) 應用。
    2. 備忘錄 應用中輸入 URL。
    3. 點選生成的連結。

    如果成功,Flutter 應用將啟動並顯示其詳情頁面。

    Deeplinked Simulator screenshot

查詢原始碼

#

您可以在 GitHub 倉庫中找到 deeplink_cookbook 示例的原始碼。