Basic Integration of Firebase Remote Config into an iOS App

I recently attended iOSDEVUK in Aberystwyth. There was a fascinating talk by Todd Kerpleman (@toddkerpleman) about using Firebase to perform A/B testing.

The technology is pretty cool so I created a demo project. If you’re looking to integrate Firebase into your app then this tutorial looks specifically at how to integrate Firebase Remote Config into your app.

  1. Start by creating a brand new app in xCode. Make sure it’s set up within a workspace with CocoaPods installed and setup.
  2. Add pod ‘Firebase/RemoteConfig’ to your pods file and run pod install
  3. Make a note of your app’s bundle identifier.
  4. Go to http://firebase.google.com and sign up for an account.
  5. Select Create New Project, give your project a name and select your region.screen-shot-2016-09-07-at-14-10-57
  6. Once in the project, select the option to Add Firebase to your iOS App.
    screen-shot-2016-09-07-at-14-12-42
  7. Enter your app’s bundle identifier and if the app is going to production also enter the app store identifier for your app.
  8. The file GoogleService-Info.plist will be downloaded to your computer.
  9. Drag this file into your xCode projectYou’re now ready to start interacting with Firebase from within iOS.For the purposes of this tutorial I am going to create an app that downloads Constants from Firebase and changes the appearance of a button within my app based upon the retrieved values.
  1. Modify your apps storyboard and add a simple UIButton. Connect the UIButton to your class file as an IBOutlet.We now need to set the app up to download our constants values from Firebase. I’ve decided that I would like the app to download values from Firebase on each launch.
  1. Add a plist to your app and name it plist
  1. Add a new CocoaTouch class to xCode with a subclass of NSObject. Name the file Constants.swift
  1. At the top of the class add the line of code import Firebase
  1. Create a new mutable variable remoteConfig with a type of FIRRemoteConfig

 var remoteConfig:FirRemoteConfig!

  1. Add a new function to the class and call it createDefaults. This function is going to create an instance of the default firebase values and make them accessible by your app.
    screen-shot-2016-09-07-at-14-31-38
  2. Add a new function called setupApp. This function will retrieve the RemoteConfig from Firebase. This function will call createDefaults() to instantiate the default Firebase values.screen-shot-2016-09-07-at-14-33-15The statement self.remoteConfig.activateFetched() instructs your app to replace the local defaults with those fetched from the server.
  1. In the AppDelegate.swift file import Firebase
  1. In the AppDelegate.swift file call FIRApp.configure() from within didFinishLaunchingWithOptions
  1. In the AppDelegate.swift file call Constants().setupAPP() from within didFinishLaunchingWithOptions
  1. If you build and run your app you should now see a log in the console that reads “Config Fetched”
  1. You now need to retrieve key values from the default data returned from Firebase. In Constants.swift create a new function called getButtonText() which returns a string value.screen-shot-2016-09-07-at-14-40-15
  1. In Firebase create a new RemoteConfig property called “ButtonText”. In the example below I’ve also created properties for ButtonTextColor and ButtonColor.screen-shot-2016-09-07-at-14-43-14
  1. Calling Constants().getButtonText from the view controller will then return the value to be used as the label for the Button.screen-shot-2016-09-07-at-14-48-10
  1. You can now create properties in Firebase for any setting within your app easily retrieve it and use it anywhere within your app.

The sample project for this app is available on Github https://github.com/stephenwestgarth/FirebaseRemoteConfigSampleApp/

You will need to replace the GoogleService-Info.plist with your own created in Firebase if you want to see anything other than the values that I have created.

 

Share: Pinterest