Chenglu
Chenglu

Reputation: 884

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

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

Upvotes: 11

Views: 7116

Answers (4)

Pranav Gupta
Pranav Gupta

Reputation: 811

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()
        }
    }
}

Upvotes: 7

mofojed
mofojed

Reputation: 1393

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)

Upvotes: 0

ausan
ausan

Reputation: 116

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.

Upvotes: 1

Alex Bauer
Alex Bauer

Reputation: 13613

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

Upvotes: 12

Related Questions