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.
- Start by creating a brand new app in xCode. Make sure its set up within a workspace and that you have CocoaPods installed and setup.
- Add pod ‘Firebase/RemoteConfig’ to your pods file and run pod install
- Make a note of your apps bundle identifier.
- Go to http://firebase.google.com and sign up for an account.
- Select Create New Project, give your project a name and select your region.
- Once in the project select the option to Add Firebase to your iOS App.
- Enter your apps bundle identifier and if the app is going to production also enter the app store identifier for your app.
- The file GoogleService-Info.plist will be downloaded to your computer.
- 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.
- 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.
- Add a plist to your app and name it plist
- Add a new CocoaTouch class to xCode with a subclass of NSObject. Name the file Constants.swift
- At the top of the class add the line of code import Firebase
- Create a new mutable variable remoteConfig with a type of FIRRemoteConfig
- 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.
- 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.The statement self.remoteConfig.activateFetched() instructs your app to replace the local defaults with those fetched from the server.
- In the AppDelegate.swift file import Firebase
- In the AppDelegate.swift file call FIRApp.configure() from within didFinishLaunchingWithOptions
- In the AppDelegate.swift file call Constants().setupAPP() from within didFinishLaunchingWithOptions
- If you build and run your app you should now see a log in the console that reads “Config Fetched”
- 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.
- In Firebase create a new RemoteConfig property called “ButtonText”. In the example below I’ve also created properties for ButtonTextColor and ButtonColor.
- Calling Constants().getButtonText from the view controller will then return the value to be used as the label for the Button.
- 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.