We are adding Firebase-Deeplinks to our IOS-project, so that the app can be started via deeplink.

The deeplink-feature itself work fine so far, and so does the default app launch routine. But making both startRoutines work side by side gives me some headache.

What I am trying to achieve get's obvious looking at this code snippet.

func application(_:didFinishLaunchingWithOptions:) {
   FirebaseApp.configure()

   if "deeplink" {
      return true 
   }
   defaultAppLaunch() // no deeplink
   return true 
}

If there is a deeplink one of these appDelegate-functions is called:

func application(:continueUserActivity:restorationHandler:) {
    handleDeeplink()
    return true
}

func application(:openURL:options:) {
    handleDeeplink()
    return true  
}

So how do I know at application(_:didFinishLaunchingWithOptions:) if I can call defaultAppLaunch()?

I know there is the launchOptions-Argument in but in my case it is always nil, at least when running the app via XCode. And also the Firebase-Documentation says nothing about launchOptions to be set by Firebase-Deeplinks.

Help is highly appreciated.

2

There are 2 best solutions below

1
On

I'm referencing the Firebase docs in handling dynamic links for iOS: Firebase docs for receiving dynamic links

Next, in the application:continueUserActivity:restorationHandler: method, handle links received as Universal Links when the app is already installed (on iOS 9 and newer):

func application(_ application: UIApplication, continue userActivity: NSUserActivity,
                 restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
  let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
    // ...
  }

  return handled
}

Finally, in the application:openURL:sourceApplication:annotation: (iOS 8 and older) and application:openURL:options: (iOS 9 and up) methods, handle links received through your app's custom URL scheme. These methods are called when your app receives a link on iOS 8 and older, and when your app is opened for the first time after installation on any version of iOS.

@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
  return application(app, open: url,
                     sourceApplication: options[UIApplicationOpenURLOptionsKey.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
}

But you did mention that the app is currently only being run on Xcode (and I'm guessing iOS Simulator, maybe you can try it on a test device too!)

0
On

TL;DR

You can't know that your app was opened using deeplinks through App Delegate DidFinishLaunching.


Explaination:

App delegate did finish launch is always called, regardless if app was opened normally or via deeplinks. so you can't know through app delegate

Instead, you can know that app was opened through deeplinks if the following delegate function is called.

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
}

and you should handle the deeplinks functionality in the same function