要接收您創建的Firebase 動態鏈接,您必須在應用中包含動態鏈接 SDK,並在應用加載時調用handleUniversalLink:
和dynamicLinkFromCustomSchemeURL:
方法以獲取動態鏈接中傳遞的數據。
先決條件
在開始之前,請確保將Firebase 新增至您的 iOS 專案。
設定 Firebase 和動態連結 SDK
使用 Swift Package Manager 安裝和管理 Firebase 相依性。
- 在 Xcode 中,開啟應用程式項目,導覽至File > Add Packages 。
- 出現提示時,新增 Firebase Apple 平台 SDK 儲存庫:
- 選擇動態連結庫。
- 將
-ObjC
標誌新增至目標建置設定的「其他連結器標誌」部分。 - 為了獲得動態連結的最佳體驗,我們建議在您的 Firebase 專案中啟用 Google Analytics ,並將適用於 Google Analytics 的 Firebase SDK 新增至您的應用程式。您可以選擇沒有 IDFA 收集或有 IDFA 收集的庫。
- 完成後,Xcode 將自動開始在背景解析並下載您的依賴項。
https://github.com/firebase/firebase-ios-sdk.git
現在,執行一些設定步驟:
- 在Firebase 控制台中,開啟動態連結部分。若出現提示,請接受服務條款。
確保在應用程式的設定中指定了應用程式的 App Store ID 和應用程式 ID 前綴。若要查看和編輯應用程式的設置,請前往 Firebase 專案的設定頁面並選擇您的 iOS 應用程式。
您可以透過開啟以下 URL 來確認您的 Firebase 專案是否已正確配置為在 iOS 應用程式中使用動態連結:
https://your_dynamic_links_domain/apple-app-site-association
如果您的應用程式已連接,則
apple-app-site-association
檔案包含對應用程式的應用程式 ID 前綴和捆綁包 ID 的參考。例如:{"applinks":{"apps":[],"details":[{"appID":"1234567890.com.example.ios","paths":["NOT /_/*","/*"]}]}}
如果
details
欄位為空,請仔細檢查您是否指定了應用程式 ID 前綴。請注意,您的應用程式 ID 前綴可能與您的團隊 ID 不同。可選:停用 Dynamic Links SDK 對 iOS 貼簿的使用。
預設情況下,動態連結 SDK 使用貼上板來提高安裝後深層連結的可靠性。透過使用貼上板,動態連結可以確保當用戶打開動態連結但需要先安裝您的應用程式時,用戶在安裝後首次打開應用程式時可以立即轉到原始連結內容。
這樣做的缺點是使用黏貼簿會在 iOS 14 及更高版本上觸發通知。因此,當用戶第一次打開您的應用程式時,如果貼簿包含 URL,他們將看到您的應用程式存取貼上的通知,這可能會導致混亂。
若要停用此行為,請編輯 Xcode 專案的
Info.plist
檔案並將FirebaseDeepLinkPasteboardRetrievalEnabled
鍵設為NO
。
在您的應用程式中打開動態鏈接
- 在應用程式的 Xcode 專案的「資訊」標籤中,建立一個用於動態連結的新 URL 類型。將標識符欄位設為唯一值,並將URL 方案欄位設定為捆綁包標識符,這是動態連結使用的預設 URL 方案。
- 在應用程式 Xcode 專案的「功能」標籤中,啟用「關聯網域」並將下列內容新增至「關聯網域」清單:
applinks:your_dynamic_links_domain
- 如果您想要接收具有完全自訂網域的動態鏈接,請在 Xcode 專案的
Info.plist
檔案中建立名為FirebaseDynamicLinksCustomDomains
的鍵,並將其設定為應用程式的動態連結 URL 前綴。例如:FirebaseDynamicLinksCustomDomains https://example.com/promos https://example.com/links/share - 在
UIApplicationDelegate
中導入FirebaseCore
模組,以及應用程式委託使用的任何其他Firebase 模組。例如,要使用 Cloud Firestore 和身份驗證:斯威夫特使用者介面
import SwiftUI import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
迅速
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 在應用程式委託的
application(_:didFinishLaunchingWithOptions:)
方法中設定FirebaseApp
共享實例:斯威夫特使用者介面
// Use Firebase library to configure APIs FirebaseApp.configure()
迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 如果您使用 SwiftUI,則必須建立應用程式委託並透過
UIApplicationDelegateAdaptor
或NSApplicationDelegateAdaptor
將其附加到您的App
結構。您還必須停用應用程式委託調配。有關更多信息,請參閱SwiftUI 說明。斯威夫特使用者介面
@main struct YourApp: App { // register app delegate for Firebase setup @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { NavigationView { ContentView() } } } }
- 接下來,在
application:continueUserActivity:restorationHandler:
方法中,在應用程式已安裝時處理作為通用連結接收的連結:迅速
注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { let handled = DynamicLinks.dynamicLinks() .handleUniversalLink(userActivity.webpageURL!) { dynamiclink, error in // ... } return handled }
Objective-C
注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler: #if defined(__IPHONE_12_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0) (nonnull void (^)(NSArray<id<UIUserActivityRestoring>> *_Nullable))restorationHandler { #else (nonnull void (^)(NSArray *_Nullable))restorationHandler { #endif // __IPHONE_12_0 BOOL handled = [[FIRDynamicLinks dynamicLinks] handleUniversalLink:userActivity.webpageURL completion:^(FIRDynamicLink * _Nullable dynamicLink, NSError * _Nullable error) { // ... }]; return handled; }
- 最後,在
application:openURL:options:
中處理透過應用程式的自訂 URL 方案接收的連結。當您的應用程式安裝後第一次開啟時,會呼叫此方法。如果您的應用程式首次啟動時未找到動態鏈接,則將呼叫此方法,並將
DynamicLink
的url
設為nil
,表示 SDK 無法找到匹配的待處理動態連結。迅速
注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。@available(iOS 9.0, *) func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool { return application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey .sourceApplication] as? String, annotation: "") } func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) { // Handle the deep link. For example, show the deep-linked content or // apply a promotional offer to the user's account. // ... return true } return false }
Objective-C
注意:此產品不適用於 macOS、Mac Catalyst、tvOS 或 watchOS 目標。- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options { return [self application:app openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url]; if (dynamicLink) { if (dynamicLink.url) { // Handle the deep link. For example, show the deep-linked content, // apply a promotional offer to the user's account or show customized onboarding view. // ... } else { // Dynamic link has empty deep link. This situation will happens if // Firebase Dynamic Links iOS SDK tried to retrieve pending dynamic link, // but pending link is not available for this device/App combination. // At this point you may display default onboarding view. } return YES; } return NO; }