6

I've set up universal links with the Branch SDK. The links are opening the app correctly, and application:continueUserActivity:restorationHandler: is called, but not `application:openURL:options:'

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    Branch.getInstance().application(app, open: url, options: options)
    return true
}

The deprecated application:openURL:sourceApplications:annotation is also not called. didFinishLaunchingWithOptions and willFinishLaunchingWithOptions both return true.

What could possibly causing openURL to not be called when the app opens from tapping a universal link?

timgcarlson
  • 3,017
  • 25
  • 52
  • try this. delete the app and install again. – Mohammad Sadiq Jul 28 '17 at 20:53
  • @MohammadSadiq Deleted app, deleted derived data, performed a clean, rebuilt... still not calling that method when the app opens. – timgcarlson Jul 28 '17 at 20:59
  • Its clearly stated in [the documentation](https://developer.apple.com/library/content/documentation/General/Conceptual/AppSearch/UniversalLinks.html), that for Universal Links, the `continueUserActivity` method is called. As simple as that. – Losiowaty Jul 28 '17 at 21:50
  • @MohammadSadiq you save my life, 老铁666 – c0ming Sep 10 '19 at 06:17

1 Answers1

11

Clay from Branch here.

The application:openURL:sourceApplications:annotation function (now deprecate to application(_:open:options:)) is actually only called in response to the old Apple Linking system of standard URI schemes.

Universal links are actually handled within the application(_:continue:restorationHandler:) function.

// Respond to URI scheme links
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    // pass the url to the handle deep link call
    Branch.getInstance().application(app, open: url, options: options)

    // do other deep link routing for the Facebook SDK, Pinterest SDK, etc
    return true
}

// Respond to Universal Links
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    // pass the url to the handle deep link call
    Branch.getInstance().continue(userActivity)

    return true
}

Your deep link handling should mostly be dealt with in your handler callback:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  let branch: Branch = Branch.getInstance()
  branch?.initSession(launchOptions: launchOptions, deepLinkHandler: { params, error in
    if error == nil {
        // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
        print("params: %@", params.description)
    }
   })
   return true
}
clayjones94
  • 2,648
  • 17
  • 26
  • Thanks for the response Clay. So as long as I'm targeting iOS 10+, I don't actually need the `application(_:open:options:)` function, correct? In `application(_:continue:restorationHandler:)`, can you explain the purpose of `Branch.getInstance().continue(userActivity)`? I'm not quite sure what the point of it is if I'm just going to handle the link in Branch handler callback. – timgcarlson Jul 31 '17 at 18:02
  • 2
    Correct, you do not need `application(_:open:options:)` if you are targeting iOS 10+. The `Branch.getInstance().continue(userActivity)` allows Branch to handle restoration from an `NSUserActivity`. The userActivity is the `NSUserActivity` that caused the app to be opened. This method is how Branch processes the Universal Link. You only need to worry about the call back in the `branch?. initSession` function. That is where all of the link processing is handled – clayjones94 Jul 31 '17 at 22:11