Firebase Remote Config is used to change the application behavior without publishing update of application.
Basic setup is to create project at firebase console.
I am adding a video here to setup the firebase remote config.
Add core firebase to your project
Follow the steps available at : Add Firebase to your iOS Project
Steps to add remote config to your app
- Installation
Update your project with required cocoapods
123456789# Uncomment the next line to define a global platform for your projectplatform :ios, '9.0'target 'FireSwiftRemoteConfig' do# Comment the next line if you're not using Swift and don't want to use dynamic frameworksuse_frameworks!# Pods for FireSwiftRemoteConfigpod 'Firebase/Core'pod 'Firebase/RemoteConfig'end
Run pod install from terminal and open the created .xcworkspace file. - Configure Firebase Module
Just import firebase module and apply configure method. It will configure by itself by using GoogleService-Info.plist.
1234567891011import UIKitimport Firebase@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {// Firebase ConfigurationFIRApp.configure()return true}} - Configure remote config
Create Remote Config object, as shown in the following example:
12345var remoteConfig: FIRRemoteConfig!override func viewDidLoad() {super.viewDidLoad()remoteConfig = FIRRemoteConfig.remoteConfig()}
Create an plist file for default values of configuration and set it to remote config:
123let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true)remoteConfig.configSettings = remoteConfigSettings!remoteConfig.setDefaultsFromPlistFileName("FireSwiftRemoteConfigDefaults")
FireSwiftRemoteConfigDefaults.plist with sampleURL key:
12345678<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>sampleURL</key><string>http://google.com</string></dict></plist>
Use current default key from your defaults set on plist file and send fetch request in remote config to get the configuration keys set on the firebase console:
1234567891011121314151617func fetchConfiguration() {lblResult.text = remoteConfig[sampleURLConfigKey].stringValuevar expirationDuration = 3600if remoteConfig.configSettings.isDeveloperModeEnabled {expirationDuration = 0}remoteConfig.fetch(withExpirationDuration: TimeInterval(expirationDuration)) { (status, error) -> Void inif status == .success {print("Config fetched!")self.remoteConfig.activateFetched()} else {print("Config not fetched")print("Error \(error!.localizedDescription)")}lblResult.text = remoteConfig[sampleURLConfigKey].stringValue}}
Note : If in developer mode cacheExpiration is set to 0 so each fetch will retrieve values from the server. The default expiration duration is 43200 (12 hours).
All Done.
Sample code available at Github. There are many other things with firebase, I will try give update in next tutorials.
If you like then Buy me a coffee ☕️
Happy Coding 🙂