Chapter 10 · Firebase Remote Config Basics
Subchapter 10.2
references/ios_setup.mdMarkdown3 KBView on GitHub
Important references:
firebase-basics skills, particularly those for iOS setup,
before proceeding.xcode-project-setup skills.Use the firebase-tools CLI to set up the project if necessary.
PRODUCT_BUNDLE_IDENTIFIER value in the .pbxproj file or the
Info.plist file.npx -y firebase-tools@latest projects:create <project-id> --display-name="My Awesome App"npx -y firebase-tools@latest apps:create IOS <bundle-id>xcode-project-setup skill to obtain the config and link.Install the Remote Config and Analytics SDKs using the Swift package manager.
Install the FirebaseRemoteConfig and FirebaseAnalytics packages from the
https://github.com/firebase/firebase-ios-sdk.git (opens in a new tab)
repository.
Modify the application’s entry point to initialize Firebase. Refer to the iOS setup reference in the firebase-basics skill.
The following steps cover the essential patterns for using Remote Config effectively in your iOS app.
Define default values so your app behaves as intended before it connects to the backend. Create a property list file (e.g., RemoteConfigDefaults.plist):
```xml
<?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>welcome_message</key>
<string>Welcome to the app!</string>
<key>is_feature_enabled</key>
<false/>
</dict>
</plist>
```Then, initialize the SDK and set the defaults:
```swift
import FirebaseRemoteConfig
let remoteConfig = RemoteConfig.remoteConfig()
remoteConfig.setDefaults(fromPlist: "RemoteConfigDefaults")
```To retrieve values from the cloud and apply them to your app:
```swift
remoteConfig.fetchAndActivate { (status, error) in
if status == .successFetchedFromRemote || status == .successUsingPreFetchedData {
print("Config fetched and activated!")
} else {
print("Config not fetched")
}
// Access a value
let message = remoteConfig.configValue(forKey: "welcome_message").stringValue
}
```