9

As Branch said in its docs:

For more advanced implementations, you may want to specify keys for both Test and Live environments (for example, if you are building a custom switch to automatically select the correct key depending on compiler schemes).

Open your Info.plist file in Xcode, change the branch_key entry to a Dictionary, and create two subentries for your keys:

My question is: How do I build a custom switch to automatically select the correct key depending on compiler schemes? I understand I might use #if DEBUG to define the environment, but I don't understand is where do I tell branch which key it should use? Or branch will simply detect it automatically?

Thank you so much![screenshot of branch config in plist file

Chenglu
  • 844
  • 1
  • 9
  • 17

4 Answers4

11

Alex from Branch.io here: #if DEBUG is the best, approach, and you actually just need to switch out your singleton call. Instead of

let branch: Branch = Branch.getInstance(); // Swift
Branch *branch = [Branch getInstance]; // Objective C

you'll use

let branch: Branch = Branch.getTestInstance(); // Swift
Branch *branch = [Branch getTestInstance]; // Objective C
Alex Bauer
  • 13,147
  • 1
  • 27
  • 44
  • I suppose that a debug vs release build should consistently use either `getTestInstance()` or `getInstance()`. I find debug builds of our app calling `Branch.getTestInstance().initSessionWithLaunchOptions(...)` when launching, but then using `Branch.getInstance()` later and suspect that's a problem. Do those instances coexist, do they conflict in any way? Or maybe its ok, maybe the instance that's initialized is the one that gets returned from either `get` accessor? I'd appreciate confirmation. – Pierre Houston Nov 04 '16 at 00:00
  • You need to be consistent about which environment gets used. Branch sees the Live and Test instances as totally separate apps, so the sessions will not be merged and you'll run into bizarre behavior like missing link data and incorrect identities/attribution. – Alex Bauer Nov 04 '16 at 01:10
  • Thanks this reminds me how important it is to read EVERY SINGLE DETAIL. :) I had missed the "getTestInstance" portion of the SDK integration guide. – nenchev Dec 09 '16 at 22:20
  • Depending on how long ago you read the guide, it might not have existed yet! Glad everything is working now :) – Alex Bauer Dec 10 '16 at 00:44
  • @AlexBauer Could you please help with the following similar issue from branch https://github.com/BranchMetrics/android-branch-deep-linking-attribution/issues/958 – abhishek maharajpet Jan 06 '22 at 14:34
6

As of May 2018 :

Branch.getTestInstance() is deprecated. Please use below extension to proceed:

extension Branch {
    class var instance:Branch {
        get {
            #if DEBUG
                Branch.setUseTestBranchKey(true)
            #endif
            return Branch.getInstance()
        }
    }
}
Thomas Besnehard
  • 2,106
  • 3
  • 25
  • 47
Pranav Gupta
  • 741
  • 9
  • 10
  • If you need to support multiple configurations / schemes, you can use `return Branch.getInstance("")` This allows for more keys to be used and is independent from the plist file – Mostafa ElShazly Mar 11 '22 at 09:36
1

You can pass NSString to getInstance. I was using it like that:

if (Debug) {
[Branch getInstance:@"key_test_lalala"];
}

else {
[Branch getInstance:@"key_live_lalala"];
}

In this case you also don't need to have branch_key in plist.

However, as a side note, recently we had a problem that branch links were not working with test key anymore, and the reply from support was that we should not use test keys anymore.

ausan
  • 106
  • 1
  • 8
0

Adding on to Alex Bauer's response, I created an extension to return the proper instance of Branch:

import Branch

extension Branch {
    class var instance:Branch {
        get {
            #if DEBUG
                return Branch.getTestInstance()
            #else
                return Branch.getInstance()
            #endif
        }
    }
}

Usage:

Branch.instance.initSession(launchOptions: launchOptions)
mofojed
  • 1,342
  • 9
  • 11