Search code examples
swiftfirebaseswiftuifirebase-dynamic-links

Clicking the Firebase dynamic link does not call the application (_: continue: restorationHandler :) method in AppDelegate


Using Firebase Dynamic Links, I want to be able to click a link to open an iOS app.

The app is under development and is not yet on the AppStore.

I set it while looking at the document, but even if the app is started by clicking the link, the application (_: continue: restorationHandler :) method in AppDelegate is not called.

I set it according to the following procedure. Is something wrong?  

  1. Add "pod 'Firebase / DynamicLinks'" to Podfile and install

  2. Open the .xcworkspace file

  3. Create Dynamic Links “https://XXXX.page.link/test

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

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

  5. Create a URL type to use for dynamic links enter image description here

  6. Enable "Associated Domains" and add "applinks: XXXX.page.link" enter image description here

  7. Implement AppDelegate as below

    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
        }
    }
    

Solution

  • I was calling the scene method of 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!)") 
        }
        ...
    }