Branch.io: how to switch between live and test environment in iOS
Asked Answered
P

4

11

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

Pyrostat answered 16/6, 2016 at 22:35 Comment(1)
I found this link helpful: docs.branch.io/apps/ios/#simulate-an-installAntecedents
S
12

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
Shoffner answered 17/6, 2016 at 0:44 Comment(5)
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.Gleich
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.Shoffner
Thanks this reminds me how important it is to read EVERY SINGLE DETAIL. :) I had missed the "getTestInstance" portion of the SDK integration guide.Backhouse
Depending on how long ago you read the guide, it might not have existed yet! Glad everything is working now :)Shoffner
@AlexBauer Could you please help with the following similar issue from branch github.com/BranchMetrics/…Saturday
G
7

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()
        }
    }
}
Growler answered 31/5, 2018 at 15:44 Comment(2)
If you need to support multiple configurations / schemes, you can use return Branch.getInstance("<key>") This allows for more keys to be used and is independent from the plist fileLelalelah
Great solution. Be sure to change Branch.getInstance().initSession(launchOptions: launchOptions) with Branch.instance.initSession(launchOptions: launchOptions)Diplomat
W
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.

Wildee answered 20/6, 2016 at 15:8 Comment(0)
M
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)
Misogyny answered 28/3, 2017 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.