Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Firebase configuration at runtime on iOS

Tags:

ios

firebase

For my project, I have 2 environments:

  • test
  • prod

and 2 localizations:

  • en
  • tr

and an app for each combination: (these names do not mirror Firebase project ids)

  • test_en
  • test_tr
  • prod_en
  • prod_tr

And I have a separate plist file for each of these apps in the project, with the names Firebase-(app_type).plist ie Firebase-test_en.plist or Firebase-prod_en.plist

I am initializing FIRApp using this way:

private func ensureLoaded() {
    let options = FIROptions(contentsOfFile: Bundle.main
        .path(forResource: "Firebase-\(firebaseApplicationName)", ofType: "plist"))!

    if nil == FIRApp(named: firebaseApplicationName) {
        FIRApp.configure(withName: firebaseApplicationName, options: options)
    }
}

And it seems to work ok, until I try to actually connect to firebase database, where it says:

Firebase is not configured correctly, please call [FIRApp configure]

So my question is, how do I make firebase work where I want to dynamically change configurations? There are some other questions that describe how to change configuration based on some environment variables / compiler flags but they are all referring to changing the plist file, which changes the configuration during build time, but I want to change it dynamically during runtime. Is it supported at all, is there a way to make it work?

Thanks in advance.

like image 256
akaralar Avatar asked May 01 '17 11:05

akaralar


1 Answers

I found what the problem was, we were using FIRApp.auth().currentUser somewhere in the app, which returns the auth for "default" app and since we had multiple configs, we had no notion of a "default" app, so the configuration was incomplete. I changed that to FIRAuth(app: myApp)?.currentUser and it works as expected now.

like image 190
akaralar Avatar answered Oct 12 '22 00:10

akaralar