点击 Firebase 动态链接不会调用 AppDelegate 中的应用程序 (_: continue: recoveryHandler :) 方法

Ika*_*Ika 2 firebase swift firebase-dynamic-links swiftui

使用 Firebase 动态链接,我希望能够单击链接以打开 iOS 应用程序。

该应用程序正在开发中,尚未在 AppStore 上架。

我是一边看文档一边设置的,但是即使点击链接启动了应用程序,也不会调用AppDelegate中的应用程序(_:continue:restoreHandler:)方法。

我按照以下步骤进行设置。有什么问题吗??

  1. 将“pod 'Firebase / DynamicLinks'”添加到 Podfile 并安装

  2. 打开 .xcworkspace 文件

  3. 创建动态链接“ https://XXXX.page.link/test

  4. 访问“ https://XXXX.page.link/apple-app-site-association

    {"applinks":{"apps":[],"details":[{"appID":"XXXXXXX.[Bundle ID]","paths":["NOT /_/ ","/ "]}] }}

  5. 创建用于动态链接的 URL 类型 在此处输入图片说明

  6. 启用“关联域”并添加“应用链接:XXXX.page.link” 在此处输入图片说明

  7. 如下实现 AppDelegate

    import UIKit
    import Firebase
    import FirebaseDynamicLinks
    
    @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            FirebaseApp.configure()
            return true
        }
    
        // MARK: UISceneSession Lifecycle
    
        func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            // Called when a new scene session is being created.
            // Use this method to select a configuration to create the new scene with.
            return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        }
    
        func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
            // Called when the user discards a scene session.
            // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
            // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
        }
    
        func application(_ application: UIApplication, continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
          let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
            // ...
          }
    
          return handled
        }
    
        @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
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

Ika*_*Ika 6

我正在调用 SceneDelegate 的场景方法。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        let rootView = ContentView()

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: rootView)
            self.window = window
            window.makeKeyAndVisible()
        }

        guard let userActivity = connectionOptions.userActivities.first(where: { $0.webpageURL != nil }) else { return }
        print("url: \(userActivity.webpageURL!)")
    }

    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        print("url: \(userActivity.webpageURL!)") 
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)