July 8, 2020

How to implement Firebase remote config in iOS

By Mohit Agrawal

Change your app behavior without publishing a new release using Firebase remote config

Introduction

Suppose you want to change your app theme’s color daily, based on your random choice. So will you release a new build daily with different colors?
No, right? That is practically not possible. Here comes the Firebase remote config.

Firebase remote config allows you to change the behavior of your app on the fly, without releasing a new build.

In this firebase tutorial you will learn how to connect your iOS app to the Firebase and then the implementation of Firebase remote config.

Check out Firebase remote config in android.

Implementation

Create a new iOS project. If you have an existing project then also you will able to follow up on this tutorial.

1. Linking the app with Firebase

Open the firebase and create a new project. I have given “remote config iOS” as my firebase project name.

Go to the project settings and click on the iOS icon to add an iOS app as shown in the below screenshot.

firebase_reomte_ios_1

Now you will get something like this, Here you have to give the bundle id of your app.

firebase_reomte_ios_2

After clicking on the Register app button you will get a JSON file. Download this JSON file and add to the root of your Xcode project and add it to all targets.

firebase_reomte_ios_4

Add the json file using the Xcode.

Till here we have added our project to firebase console, now we have to add Firebase SDK to our app. To install the Firebase SDK we will use the Cocoa-pods dependency manager.

Close the Xcode and open a terminal window and navigate to the location of the Xcode project for your app and run the command pod init, it will create a pod file. Open this pod file using some editor and add pod 'Firebase/RemoteConfig'. Your pod file should look like this.

firebase_reomte_ios_6

Now run pod install. It will add firebase SDK to your iOS app

If you want to add some more firebase dependency then check this list

A new workspace fill will be created, now onwards you have to use your-project.xcworkspace only to open your project.

This is the last step 😅. Open the AppDelegate.swift and add this one line of code in didFinishLaunchingWithOptions function.

FirebaseApp.configure()

Till here we have successfully added the firebase into our project.

2. Add remote config key value in firebase

In this step, we will add a key and its value on the firebase remote config page. Open the Remote Config dashboard from the left side menu.

firebase_reomte_ios_7

I have set “welcome_text” as the key. Using this key only we will be able to fetch the corresponding data in our app.

Do not forget to publish your changes after updating the key and value.

3. Fetch and update the UILabel’s text using remote config

To demonstrate the Firebase remote config, I am taking a. very basic app design. This app will have one UILablel. I will show, how you can change its text value using the remote config.

Create a UILabel on Main.storyboard and make its reference in ViewController.swift.

Here is the complete code.

import UIKit
import Firebase

class ViewController: UIViewController {
    
    @IBOutlet weak var welcomeLabel: UILabel!
    
    var remoteConfig: RemoteConfig!

    //Your firebase key
    let welcome_key = "welcome_text"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // [START get_remote_config_instance]
        remoteConfig = RemoteConfig.remoteConfig()
        setupRemoteDefaultValues()
        
        fetchConfig()
    }
    
    //Setting up default value. This value will be used when firebase
    //has is facing some issue while fetching the updated value from the server
    func setupRemoteDefaultValues() {
        let defaults = [
            "welcome_text": "This is default" as NSObject
        ]
        remoteConfig.setDefaults(defaults)
    }

    func fetchConfig() {
      welcomeLabel.text = remoteConfig[welcome_key].stringValue

        // [START fetch_config_with_callback]
        remoteConfig.fetch(withExpirationDuration: 0) { (status, error) in
            if status == .success {
              print("Config fetched!")
              self.remoteConfig.activate() { (error) in
                // ...
              }
            } else {
              print("Config not fetched")
              print("Error: \(error?.localizedDescription ?? "No error available.")")
            }
            self.displayWelcome()
        }
        // [END fetch_config_with_callback]
    }

    func displayWelcome() {
      // [START get_config_value]
        let welcomeMessage = remoteConfig[welcome_key].stringValue
      // [END get_config_value]
      welcomeLabel.text = welcomeMessage
    }
}

When you run this code, you will get something like this.

If you have followed everything till here, you will get it running. Please let me know in the comment section if you are facing any issues. 😎

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!

FAQs

  1. How to get bundle id in iOS project?

    Click on the project icon located at the left side of the Xcode, you will get it as shown in the screenshot.firebase_reomte_ios_3