Gå til konsollen

Get started with Firebase Remote Config on iOS

You can use Firebase Remote Config to define parameters in your app and update their values in the cloud, allowing you to modify the appearance and behavior of your app without distributing an app update.

This guide walks you through the steps to get started and provides some sample code, all of which is available to clone or download from the firebase/quickstart-ios GitHub repository.

Add Remote Config to your app

  1. Install the Firebase SDK for iOS.

  2. Create the singleton Remote Config object, as shown in the following example:

    Swift

    remoteConfig = RemoteConfig.remoteConfig()
    let settings = RemoteConfigSettings()
    settings.minimumFetchInterval = 0
    remoteConfig.configSettings = settings

    Objective-C

    self.remoteConfig = [FIRRemoteConfig remoteConfig];
    FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init];
    remoteConfigSettings.minimumFetchInterval = 0;
    self.remoteConfig.configSettings = remoteConfigSettings;

This object is used to store in-app default parameter values, fetch updated parameter values from the Remote Config backend, and control when fetched values are made available to your app.

During development, it's recommended to set a relatively low minimum fetch interval. See Throttling for more information.

Set in-app default parameter values

You can set in-app default parameter values in the Remote Config object, so that your app behaves as intended before it connects to the Remote Config backend, and so that default values are available if none are set in the backend.

  1. Define a set of parameter names, and default parameter values using an NSDictionary object or a plist file.
  2. Add these values to the Remote Config object using setDefaults:. The following example sets in-app default values from a plist file:

Swift

remoteConfig.setDefaults(fromPlist: "RemoteConfigDefaults")

Objective-C

[self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"];

Get parameter values to use in your app

Now you can get parameter values from the Remote Config object. If you later set values in the Remote Config backend, fetch them, and then activate them, those values are available to your app. Otherwise, you get the in-app parameter values configured using setDefaults:. To get these values, call the configValueForKey: method, providing the parameter key as an argument.

Set parameter values

Using the Firebase console or the Remote Config REST API, you can create new backend default values that override the in-app values according to your desired conditional logic or user targeting. This section walks you through the Firebase console steps to create these values.

  1. In the Firebase console, open your project.
  2. Select Remote Config from the menu to view the Remote Config dashboard.
  3. Define parameters with the same names as the parameters that you defined in your app. For each parameter, you can set a default value (which will eventually override the in-app default value) and you can also set conditional values. To learn more, see Remote Config Parameters and Conditions.

Fetch and activate values

  1. To fetch parameter values from Remote Config, call the fetchWithCompletionHandler: or fetchWithExpirationDuration:completionHandler: method. Any values that you set on the backend are fetched and cached in the Remote Config object.
  2. To make fetched parameter values available to your app, call the activateFetched method.

For cases where you want to fetch and activate values in one call, use fetchAndActivateWithCompletionHandler:.

This example fetches values from the Remote Config backend (not cached values) and callsactivateWithCompletionHandler: to make them available to the app:

Swift

// TimeInterval is set to expirationDuration here, indicating the next fetch request will use
// data fetched from the Remote Config service, rather than cached parameter values, if cached
// parameter values are more than expirationDuration seconds old. See Best Practices in the
// README for more information.
remoteConfig.fetch(withExpirationDuration: TimeInterval(expirationDuration)) { (status, error) -> Void in
  if status == .success {
    print("Config fetched!")
    self.remoteConfig.activate(completionHandler: { (error) in
      // ...
    })
  } else {
    print("Config not fetched")
    print("Error: \(error?.localizedDescription ?? "No error available.")")
  }
  self.displayWelcome()
}

Objective-C

// TimeInterval is set to expirationDuration here, indicating the next fetch request will use
// data fetched from the Remote Config service, rather than cached parameter values, if cached
// parameter values are more than expirationDuration seconds old. See Best Practices in the
// README for more information.
[self.remoteConfig fetchWithExpirationDuration:expirationDuration completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
    if (status == FIRRemoteConfigFetchStatusSuccess) {
        NSLog(@"Config fetched!");
      [self.remoteConfig activateWithCompletionHandler:^(NSError * _Nullable error) {
        // ...
      }];
    } else {
        NSLog(@"Config not fetched");
        NSLog(@"Error %@", error.localizedDescription);
    }
    [self displayWelcome];
}];

Because these updated parameter values affect the behavior and appearance of your app, you should activate the fetched values at a time that ensures a smooth experience for your user, such as the next time that the user opens your app. See Remote Config loading strategies for more information and examples.

Throttling

If an app fetches too many times in a short time period, fetch calls are throttled and the SDK returns FIRRemoteConfigFetchStatusThrottled. Before SDK version 6.3.0, the limit was 5 fetch requests in a 60 minute window (newer versions have more permissive limits).

During app development, you might want to refresh the cache very frequently (many times per hour) to let you rapidly iterate as you develop and test your app. To accommodate rapid iteration on a project with numerous developers, you can temporarily add a FIRRemoteConfigSettings property with a low minimum fetch interval (MinimumFetchInterval) in your app.

The default and recommended production fetch interval for Remote Config is 12 hours, which means that configs won't be fetched from the backend more than once in a 12 hour window, regardless of how many fetch calls are actually made. Specifically, the minimum fetch interval is determined in this following order:

  1. The parameter in fetch(long)
  2. The parameter in FIRRemoteConfigSettings.MinimumFetchInterval
  3. The default value of 12 hours

Next steps

If you haven't already, explore the Remote Config use cases, and take a look at some of the key concepts and advanced strategies documentation, including: