3

I have successfully integrated Firebase dynamic links and when I click on dynamic link then my app is opening.

The issues I'm facing is after opening app from dynamic links, continue userActivity: method should be called, but nothing happens.

I've checked the all the possible thing but didn't recognised the issue.

I've searched the SO for this but none of the answer helped me.

My Code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

  GIDSignIn.sharedInstance().clientID = kGoogleSignInClientId
  FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

 // DynamicLinks.performDiagnostics(completion: nil)

  FirebaseApp.configure()

  return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

  if url.absoluteString.contains(kFBAppId) {

    return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)

  }

  if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
    print(dynamicLink.url ?? URL(string: "test") as Any)
    return true
  }

    return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[.sourceApplication] as? String, annotation: options[.annotation])

}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

  //This method is not getting called
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56

3 Answers3

6

I don't know whether they keep their doc. up to date or not.

I have just copy-pasted the code from the Google's official Firebase dynamic link document.

Why was the continue userActivity: method is not called?

The reason is (See the difference in following method)

Copy pasted from google doc. - Wrong one

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

}

I wrote this (without copy paste from google doc.) - Correct one

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {

}

I've made bold the difference.

This is what I was trying for many hours. It is really very frustrating for me to blindly trust on google doc.:-|

Hope this may help other.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • 2
    But even xcode auto completes like : func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool – ergunkocak Apr 28 '20 at 09:33
6

Additional answer for those using separate SceneDelegate.swift apart from AppDelegate.swift: This method in AppDelegate >

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if userActivity.activityType == CSSearchableItemActionType {

}

is now in SceneDelegate >

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {

}
Meo Flute
  • 1,211
  • 11
  • 9
  • You saved my day! Since I use SwiftUI, there is the SceneDelegate – which is not mentioned to be relevant in this context. (Neither in Firebase docs, nor anywhere else I was looking at for the last two hours.) – HelloTimo Feb 02 '20 at 16:07
  • 2
    I was a little too excited. The SceneDelegate method seems to be fired only if the app is already running in the background. Do you know where to receive a deep link in the case of a cold start (not necessarily after fresh install. The method in AppDelegate doesn't get called anymore as well if there's a SceneDelegate. – HelloTimo Feb 02 '20 at 16:37
  • Gotta love Apple not updating their official docs after they added SceneDelegate. Thanks for this! – Matias May 06 '20 at 21:29
  • For all others landing here, the solution if the app is NOT already running can be found here: https://stackoverflow.com/questions/60524108/ios-13-universal-app-links-does-not-work-when-app-is-killed-and-not-in-backgro – Leo Feb 14 '21 at 10:26
0

You need to check two times.

  1. When app. is running in the background and is opening from Link: Delegate method is:func checkForTransferedTicket(_ userActivity: NSUserActivity) { }

  2. When app. is NOT running in background and is opening from Link:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) { if let userActivity = connectionOptions.userActivities.first {
                     print(userActivity)
                 }
         }
    
zdravko zdravkin
  • 2,090
  • 19
  • 21