Firebase Remote Config

Tested with Firebase version 10.29.0 (iOS)

We assume you already have Appfigurate Library and Firebase integrated into your app with the following example remote properties created in the Firebase Console:

Add remote properties into your Configuration subclass

Update your configuration subclass to include your remote configuration properties. Provide default values for the properties in the overridden reset method. See also Supported property types.

Swift Configuration example

import Foundation
import AppfigurateLibrary

@objcMembers class MyConfiguration: APLConfiguration {
...
    @RemoteBoolProperty(remoteKey: "alwaysDarkMode", description: "Force dark mode to be always set")
    var alwaysDarkMode: Bool

    @RemoteStringPropertyEdit(remoteKey: "appTitle", description: "Title of application")
    var appTitle: String

    @RemoteIntPropertyEdit(remoteKey: "bookingDuration", description: "Duration (days) for reservation bookings")
    var bookingDuration: Int

    @RemoteDoublePropertyEdit(remoteKey: "fontSize", description: "Size of font throughout app")
    var fontSize: Double
...
    override func reset() {
        alwaysDarkMode = false
        appTitle = "Holiday finder"
        bookingDuration = 30
        fontSize = 13.0
    }
...
}

Provide remote configuration values to Appfigurate Library when requested

Appfigurate Library needs to be able to read the current remote configuration values from Firebase. Copy and paste the following code into your app.

The defaultValue parameter in the callback is not used with Firebase. See the Apply default values for Firebase section below.

Swift example

APLFetchRemoteConfiguration { remoteKey, propertyType, defaultValue in
    if propertyType == .string {
        return self.remoteConfig.configValue(forKey: remoteKey).stringValue as NSObject
    } else if propertyType == .bool {
        return self.remoteConfig.configValue(forKey: remoteKey).boolValue as NSObject
    } else { // .int || .double
        return self.remoteConfig.configValue(forKey: remoteKey).numberValue as NSObject
    }
}

Apply default values to Firebase

Apply default values to Firebase using the values you set in the reset method of your configuration subclass. The default values are used by Firebase when the remote configuration has not yet been received.

The APLConfiguration class has a method remoteDefaults which provides a Dictionary of all the remote configuration property default values that were set in your overridden reset method.

Swift example

FirebaseApp.configure()
self.remoteConfig = RemoteConfig.remoteConfig()
...
// add the following line
self.remoteConfig.setDefaults(APLConfiguration.shared().remoteDefaults())

Notify Appfigurate Library when Firebase has received remote configuration values

We need to tell Appfigurate Library that Firebase has received remote configuration, so that it can keep your Configuration subclass remote properties in sync.

Add a call to APLFlushRemoteConfiguration in any existing Firebase Remote Config activate(completion:) blocks (inside fetch(completionHandler:) and optionally addOnConfigUpdateListener(remoteConfigUpdateCompletion:).

Swift example

self.remoteConfig.fetch { status, error in
    if status == .success {
        self.remoteConfig.activate { changed, error in
            ...
            APLFlushRemoteConfiguration() // add this line
        }
    }
}

Best practice and usage

Replace all calls to Firebase Remote Config configValue(forKey:):

if remoteConfig.configValue(forKey: "alwaysDarkMode").boolValue {
   ...

with the following:

if (APLConfiguration.shared() as! MyConfiguration).alwaysDarkMode {
   ...

Last updated