Skip to content

Instantly share code, notes, and snippets.

@kylealwyn
Forked from cohenadair/firebase-sandbox.md
Created August 17, 2017 03:54
Show Gist options
  • Save kylealwyn/7a5a2f4da41b9d04b6e7921cf506280d to your computer and use it in GitHub Desktop.
Save kylealwyn/7a5a2f4da41b9d04b6e7921cf506280d to your computer and use it in GitHub Desktop.
Setting up development and production Firebase environments for iOS

Firebase Environments

Last updated: April 21st, 2017.

At the time of writing this gist (January 4th, 2017), I was unable to find true sandboxing to separate development and production environments for a Firebase project. The closest we can get is to create two separate Firebase projects -- one for development and one for production.

Pros

  • Complete separation and isolation of all Firebase features.
  • Freedom to experiment without risking the corruption of production data.

Cons

  • There is no way to copy production data to your development project (that I am aware of).
  • Any settings changes made to your development project also needs to be manually applied to your production project.

Setup

  1. Go to the Firebase console.

  2. Create two projects. That's Projects, not Apps. Apps within a project share certain features, including the Realtime Database. Make sure both projects have a Bundle ID matching your Xcode project's Bundle ID.

  3. In each project, create an iOS App, and each GoogleServices-Info.plist file to your Xcode project.

  4. Rename one of the plist files to clearly identify it as the development (or production) configuration, such as GoogleServices-Info-Dev.plist.

  5. Configure Firebase with the following code:

    Swift

    #if DEBUG
        let firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info-Dev", ofType: "plist")
    #else
        let firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
    #endif
    
    guard let options = FIROptions(contentsOfFile: firebaseConfig) else {
        fatalError("Invalid Firebase configuration file.")
    }
    
    FIRApp.configure(with: options)

    Objective-C

    #if DEBUG
        NSString *firebaseConfig = [[NSBundle mainBundle] pathForResource:"GoogleService-Info-Dev" ofType:"plist"];
    #else
        NSString *firebaseConfig = [[NSBundle mainBundle] pathForResource:"GoogleService-Info" ofType:"plist"];
    #endif
    
    FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:firebaseConfig];
    if (options == nil) {
        assert(false, "Invalid Firebase configuration file.");
    }
    
    [FIRApp configureWithOptions:options];

Now, when your app is run from Xcode, your development Firebase project will be used.

Note that if you are using FirebaseAuth, you will need to setup each authentication method (i.e. Google, Facebook) for both Firebase projects.

Credits

TheiOSChap on StackOverflow

Conclusion

If you know of a better method, please let me know. This isn't ideal, but I think it works well for what we're given.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment