為 iOS 設定通用連結
瞭解如何為使用 Flutter 構建的 iOS 應用設定通用連結。
深度連結 (Deep linking) 允許應用使用者透過 URI 啟動應用。此 URI 包含方案 (scheme)、主機 (host) 和路徑 (path),並將應用開啟至特定螢幕。
通用連結 (Universal link) 是一種 iOS 裝置專有的深度連結,僅使用 http 或 https 協議。
要設定通用連結,您需要擁有一個網站域名。作為臨時解決方案,可以考慮使用 Firebase Hosting 或 GitHub Pages。
設定好深度連結後,您可以對其進行驗證。要了解更多資訊,請參閱 驗證深度連結。
建立或修改 Flutter 應用
#編寫一個能夠處理傳入 URL 的 Flutter 應用。
此示例使用 go_router 軟體包來處理路由。Flutter 團隊維護著 go_router 軟體包。它提供了一個簡單的 API 來處理複雜的路由場景。
-
要建立新應用,請輸入
flutter create <app-name>。flutter create deeplink_cookbook -
要將
go_router軟體包新增為依賴項,請執行flutter pub addflutter pub add go_router -
要處理路由,請在
main.dart檔案中建立一個GoRouter物件main.dartdartimport '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 構建設定
#啟動 Xcode。
-
在 Flutter 專案的
ios資料夾中開啟ios/Runner.xcworkspace檔案。
新增關聯域 (Associated Domains)
#如有必要,啟動 Xcode。
點選頂部的 Runner。
在編輯器中,點選 Runner target。
點選 Signing & Capabilities。
-
要新增新域,請在 Signing & Capabilities 下點選 + Capability。
-
點選 Associated Domains。

在 Associated Domains 部分,點選 +。
-
輸入
applinks:<web domain>。將<web domain>替換為您的域名。
在您的編輯器中開啟
ios/Runner/Runner.entitlementsXML 檔案。-
在
<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> 儲存
ios/Runner/Runner.entitlements檔案。
要檢查您建立的關聯域是否可用,請執行以下步驟
如有必要,啟動 Xcode。
點選頂部的 Runner。
在編輯器中,點選 Runner target。
-
點選 Signing & Capabilities。這些域應該會出現在 Associated Domains 部分中。

您已完成應用的深度連結配置。
將應用與您的網站域名關聯
#您需要在網站域名下託管一個 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 文件,此檔案應類似於以下內容
{
"applinks": {
"apps": [],
"details": [
{
"appIDs": [
"S8QB4VV633.com.example.deeplinkCookbook"
],
"paths": [
"*"
],
"components": [
{
"/": "/*"
}
]
}
]
},
"webcredentials": {
"apps": [
"S8QB4VV633.com.example.deeplinkCookbook"
]
}
}
-
將
appIDs陣列中的一個值設定為<team id>.<bundle id>。 -
將
paths陣列設定為["*"]。paths陣列指定了允許的通用連結。使用星號*會將所有路徑重定向到 Flutter 應用。如果需要,可以將paths陣列的值更改為更適合您應用的設定。 -
將檔案託管在類似於以下結構的 URL 中。
<webdomain>/.well-known/apple-app-site-association 驗證您的瀏覽器是否可以訪問此檔案。
測試通用連結
#使用物理 iOS 裝置或模擬器測試通用連結。
-
在測試之前,請在 iOS 裝置或模擬器上安裝 Flutter 應用,在目標裝置上使用
flutter run。
完成後,Flutter 應用會顯示在 iOS 裝置或模擬器的主螢幕上。
-
如果您使用模擬器進行測試,請使用 Xcode CLI
xcrun simctl openurl booted https://<web domain>/details -
如果您使用物理 iOS 裝置進行測試
- 啟動 備忘錄 (Note) 應用。
- 在 備忘錄 應用中輸入 URL。
- 點選生成的連結。
如果成功,Flutter 應用將啟動並顯示其詳情頁面。

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