3

This is the situation: if I click one of the links that I have with custom key/values associated with it, my callback from initSessionWithLaunchOptions:andRegisterDeepLinkHandler is being triggered ok, but ‘params’ only contain the custom key/values in the case the app is fresh opened from the link. In the case the app was already running in background and I click the link, the callback is called but no parameters are present. This was working a few weeks ago and now seems to stop working. I tried upgrading to iOS SDK 0.12.1 too but no luck yet.

As a workaround, I’m trying doing a delayed sequence of calls to Branch.getInstance().getLatestReferringParams() after the app becomes active and sometimes I’m getting the parameters from there, but it’s not deterministic, sometimes it takes 2 seconds, sometimes 10 seconds, sometimes never. @AlexBauer Here's the parts of the app delegate. Btw, I don't have facebook integration in the app:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let branch: Branch = Branch.getInstance()
    branch.initSessionWithLaunchOptions(launchOptions, andRegisterDeepLinkHandler: { params, error in
        if (error == nil) {
            print(params)
            self.tryInviteFlow(params)
        } else {
            print(error.localizedDescription)
        }
    })
}

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    // pass the url to the handle deep link call
    return Branch.getInstance().continueUserActivity(userActivity);
}
vorterixe
  • 83
  • 5

1 Answers1

2

Here is a quick troubleshooting primer for issues where Branch links work properly from cold start but not when the app is becoming active after having been backgrounded...

When an app is opened from a Branch link:

  • If the app is being launched cold (i.e. it was not in the background), the entry point is didFinishLaunchingWithOptions
  • If the app was in the background and is being opened via Universal Linking (this will be the case if the device is running iOS 9.2+), the entry point will be the continueUserActivity function
  • If the app is being opened via the URI Scheme (this will be the case if the device is running pre-iOS 9 and may be the case for versions of iOS 9 up to 9.2), the entry point will be the openURL function

The same Branch init callback that is defined in didFinishLaunchingWithOptions is also called by the Branch code that is inserted into openURL and continueUserActivity functions:

  • In openURL this is: Branch.getInstance().handleDeepLink(url);
  • In continueUserActivity this is: Branch.getInstance().continue(userActivity)

If these Branch functions are not called, the Branch init callback that receives link parameters would not be called when resuming the app and link parameters would therefore not be available.

Even when these functions are called, other code within the openURL and continueUserActivity functions may prevent the Branch code from being reached. If you have other code in these code paths, verify that the appropriate Branch functions are indeed being called by testing with a breakpoint set on either the Branch function or a line of code in the init callback.

Another potential scenario that could result in issues on warm start but not cold start would be when the code that reads Branch parameters is being called outside of the Branch init callback and before that callback has been completed. If the app is busy enough in didFinishLaunchingWithOptions, the init callback may be delayed long enough that the init callback completes before the parameters are checked. Later, when the app is resuming, there may be less happening in the app and the code that checks the parameters may be checked before the init callback has completed. This is not a likely scenario, but it can happen.

To avoid this "race condition" type of situation, only check for Branch link parameters from within the init callback or after receiving a notification that has been configured to signal that the init callback is completing (you can see an example of using notifications in this manner in the TestBed-Swift project included with the SDK, here: https://github.com/BranchMetrics/ios-branch-deep-linking/tree/master/Branch-TestBed-Swift).

dwestgate
  • 1,044
  • 6
  • 12
  • Thanks, It's really helpfull. In my case I have removed the Branch SDK from the code base. And I have clicked the link and I can get the url in userActivity. But when the app is not active and click the app link and install. But I can't get the url. How to get the url? – HariKarthick Aug 11 '21 at 06:10