coolbeet coolbeet - 1 year ago 112
iOS Question

In new Firebase, how to use multiple config file in xcode?

Hey I am playing with the new firebase iOS SDK, in my project, I have only one target, I created two configurations, Debug and Release, with different bundler identifier, but seems like the config file downloaded from firebase only support one bundle identifier.

So anyone know how to use firebase within a multiple bundle identifier xcode project?

Thanks!

Answer Source

My first attempt that didn't solve the issue:

I renamed the second GoogleService-Info.json to something else, and used following code to configure Firebase from AppDelegate.

// Swift code
#if STAGING
    let firebasePlistFileName = "GoogleService-Staging-Info"
#else
    let firebasePlistFileName = "GoogleService-Info"
#endif
let firbaseOptions = FIROptions(contentsOfFile: NSBundle.mainBundle().pathForResource(firebasePlistFileName, ofType: "plist"))
FIRApp.configureWithOptions(firbaseOptions)

If I run the Staging target, I will receive Firebase's complaint about 'Could not locate configuration file: 'GoogleService-Info.plist'.'. But then it says ' Firebase Analytics v.3300000 started'.

As I checked on the Firebase dashboard, both production and staging apps have incoming user events being logged.

The above-mentioned configureWithOptions method is not documented in Firebase documentation, I figured it out by checking its source code. I'm not sure about what could be the downside of calling this. I would love to hear other approaches.

Updated with an easier solution:

1. keep the same names for both GoogleService-Info.plist
2. put one GoogleService-Info.plist inside a subfolder, say "staging"
3. add references to both files in Xcode, while link them to corresponding targets
4. just use FIRApp.configure() in your AppDelegate, done