Firebase Remote Config Tutorial for iOS

In this tutorial, you’ll learn how to make changes to your iOS app immediately without resubmitting to the App Store. By Fabrizio Brancati.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

How Remote Config Works

Oversimplified, Remote Config works similarly to a [String: Any?] dictionary living in the cloud. When your app starts, it grabs any new values it might need from the cloud, then applies them on top of any old values you might have specified as defaults.

The general process for using Remote Config looks like this:

  1. Provide Remote Config with defaults for any value you might change in the future.
  2. Fetch any new values from the cloud. You can find these stored in a cached holding pattern on your device.
  3. “Activate” those fetched values. When this happens, it applies those fetched values on top of your existing default values.
  4. Query Remote Config for values. Remote Config will give you either a value from the cloud, if it found one, or a default value based on the provided key.

Remote Config workflow

One important thing to note is that these new values you fetch are generally a subset of the default values that you supply. You can take nearly any hard-coded string, number or Boolean in your app and wire it up to use Remote Config. This gives you the flexibility to change many aspects of your app later while still keeping your actual network calls nice and small.

Enough theory. Time to put this into practice!

Using Remote Config

First, select the Utilities folder in the Project navigator in Xcode. Right-click to create a new file. Select Swift file. Name it RCValues.swift and create it in the default folder suggested by Xcode.

Add the following to the end of the file:

import Firebase

class RCValues {
  static let sharedInstance = RCValues()

  private init() {
    loadDefaultValues()
  }

  func loadDefaultValues() {
    let appDefaults: [String: Any?] = [
      "appPrimaryColor": "#FBB03B"
    ]
    RemoteConfig.remoteConfig().setDefaults(appDefaults as? [String: NSObject])
  }
}

Here, you use the Singleton pattern for RCValues. You’ll be able to access the values from the sharedInstance anywhere in your project. Inside loadDefaultValues(), you’re passing along a set of keys and values to Remote Config as defaults. Right now, you’re supplying only one value, but don’t worry, you’ll add more later.

Fetching Values From the Cloud

Next, you need to ask Remote Config to fetch new values from the cloud. Add the following method below loadDefaultValues(), just before the closing curly brace of the class:

func activateDebugMode() {
  let settings = RemoteConfigSettings()
  // WARNING: Don't actually do this in production!
  settings.minimumFetchInterval = 0
  RemoteConfig.remoteConfig().configSettings = settings
}

By default, Remote Config will cache any values it retrieves from the cloud for about 12 hours. The client-side throttle ensures that you don’t ping the service too frequently. In a production app, this is probably fine. But when you’re doing development — or following a Firebase Remote Config tutorial online — this can make it really tough to test new values. So instead, you’re specifying a minimumFetchInterval of 0 to ensure you never use the cached data.

Add the following method to fetch these values:

func fetchCloudValues() {
  // 1
  activateDebugMode()

  // 2
  RemoteConfig.remoteConfig().fetch { [weak self] _, error in
    if let error = error {
      print("Uh-oh. Got an error fetching remote values \(error)")
      // In a real app, you would probably want to call the loading 
      // done callback anyway, and just proceed with the default values. 
      // I won't do that here, so we can call attention
      // to the fact that Remote Config isn't loading.
      return
    }

    // 3
    RemoteConfig.remoteConfig().activate { _, _ in
      print("Retrieved values from the cloud!")
    }
  }
}

Here’s what’s happening in that code:

  1. By enabling debug mode, you’re telling Remote Config to bypass the client-side throttle. For development purposes or testing with your 10–person team, this is fine. But if you launch this app to the public with your millions of adoring fans, you’re going to hit the server-side throttle pretty quickly, and Remote Config will stop working. This is the whole reason you have a client-side throttle in the first place. Before you launch this app for real, make sure you disable debug mode and set your minimumFetchInterval to something a little more reasonable, like 43200 — that’s 12 hours to you and me.
  2. If Remote Config encounters an error during the fetch process, it will exit before applying the fetched values. Don’t worry about the warning for weak self. You’ll add code to address the warning later in this tutorial.
  3. If the values are downloaded successfully from the cloud, then Remote Config will use the new values instead of the default ones hard-coded in the app.

Add the following to the end of init() to call your new method:

fetchCloudValues()

Running Your Code

Open AppDelegate.swift and add the following to application(_:didFinishLaunchingWithOptions:), below FirebaseApp.configure():

_ = RCValues.sharedInstance

The underscore character means you’re not planning to use the name of the constant. Simply by accessing the value of sharedInstance, you initialize it and populate its default values.

Build and run your app, and you should see the following line in your debug console:

Retrieved values from the cloud!

Using Remote Config Values

Now that you’re downloading these values, try printing them to the console. Open RCValues.swift, then add the following to fetchCloudValues(), right after the “Retrieved values from the cloud” line:

print("""
  Our app's primary color is \
  \(RemoteConfig.remoteConfig().configValue(forKey: "appPrimaryColor"))
  """)

The above code will grab the appropriate value for your appPrimaryColor key.

Build and run your app. You should see a line like this:

Our app's primary color is <FIRRemoteConfigValue: 0x61000003ece0>

Well, that’s somewhat helpful, but you’re kind of hoping for a string value.

Remote Config retrieves values as RemoteConfigValue objects. You can think of these as wrappers around the underlying data, which is represented internally as a UTF8-encoded string. You’ll almost never use this object directly. Instead, you’ll call a helper method like numberValue or boolValue to retrieve the actual value you want.

Using helper methods to retrieve the desired value

Replace the line you just added with:

let appPrimaryColorString = RemoteConfig.remoteConfig()
  .configValue(forKey: "appPrimaryColor")
  .stringValue ?? "undefined"
print("Our app's primary color is \(appPrimaryColorString)")

Build and run your app. This time you’ll see:

Our app's primary color is #FBB03B

That’s more like it. Remote Config provides you the default value of a hex color code that you supplied earlier.