Overview

Remote Configuration allows you to manage parameters online. With the service, you can change the behavior and appearance of your app online without requiring users to update the app. Remote Configuration provides the server, console, and client SDK. By integrating the client SDK, your app can periodically fetch parameters and their values configured on the console to modify the app's behavior and appearance.

What You Will Create

In this codelab, you will create an app that integrates the Remote Configuration service and set related parameters. By calling APIs of the SDK, you can fetch parameter values from Remote Configuration.
After your app fetches parameters, you can call the update API to make them take effect immediately or as scheduled, based on your service requirements in various scenarios.

What You Will Learn

Development Environment Requirements

Device Requirements

To integrate Remote Configuration of AppGallery Connect, you must complete the following preparations:

For details, please refer to Configuring App Information in AppGallery Connect.

Enabling Remote Configuration

To integrate the Remote Configuration SDK, you need to enable Remote Configuration. For details, please refer to Enabling Remote Configuration.

Integrating the Remote Configuration SDK

If you are using Xcode, you need to integrate the Remote Configuration SDK into your Xcode project with CocoaPods.
1.Add the AppGallery Connect configuration file of the app to your Xcode project.
1) Sign in to AppGallery Connect and click My projects.
2) Find your project from the project list and click the app for SDK integration on the project card.
3) Go to Project settings > General information, and click agconnect-services.plist under App information to download the configuration file.
4) Copy the agconnect-services.plist file to the app directory of your Android Studio project.

2.Create a Podfile.
Open the CLI and navigate to the location of the Xcode project. Then, create a Podfile. Skip this step if a Podfile already exists.

cd project-directory pod init

3.Edit the Podfile.
1) Integrate the Remote Configuration SDK.
Edit the Podfile to add the pod dependency of Remote Configuration.

target 'AGRemoteDemo' do pod 'AGConnectRemoteConfig' end

2) Install the pod and open the .xcworkspace file to view the project.

pod install

You can create a page in your Xcode project and design the UI according to the following figure, with three buttons required: The first button is tapped to fetch settings from Remote Configuration and make settings take effect immediately. The second button is tapped to fetch settings from Remote Configuration and make settings take effect upon next launch. The third button is tapped to clear all parameter values including default values and values on the cloud.

You can set in-app default parameter values in a Remote Configuration instance so that your app can run properly before being connected to Remote Configuration, and in-app default parameter values are used if they are not set on the console.

NSDictionary *defaultConfig = @{@"test1":@"value1", @"test2":@(2)}; [[AGCRemoteConfig sharedInstance] applyDefaults:defaultConfig];
  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project from the project list and click your app on the project card.
  3. Go to Grow > Remote Configuration. On the displayed page, click the Parameter management tab and click Add parameter.
  4. On the Add parameter page, enter the default parameter name and default value, that is, the on-cloud key and value fetched by the SDK. Click Save, return to the Remote Configuration page, and click Release.

1.Initialize the AppGallery Connect SDK.
Import header files to the AppDelegate.m file of the project and add code for initialization.

#import "AppDelegate.h" #import <AGConnectCore/AGConnectCore.h> @implementation AppDelegate - (BOOL)Application:(UIApplication *)Application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after Application launch. [AGCInstance startup];// Initialization return YES; } ... @end

2.Apply a parameter value immediately.
On the new page that is displayed after the Mode 1 button is tapped, the NSDictionary object is used as the default value. Call the fetch API to fetch the value of AGCConfigValues, and call the apply API in the returned callback to make the parameter take effect immediately.

#import "FirstModeViewController.h" #import <AGConnectRemoteConfig/AGConnectRemoteConfig.h> @implementation FirstModeViewController - (void)viewDidLoad { [super viewDidLoad]; NSDictionary *defaultConfig = @{@"test1":@"value1", @"test2":@(2)}; [[AGCRemoteConfig sharedInstance] applyDefaults:defaultConfig]; [[[[AGCRemoteConfig sharedInstance] fetch] addOnSuccessCallback:^(AGCConfigValues * _Nullable result) { [[AGCRemoteConfig sharedInstance] apply:result]; [self showAllValue]; NSLog(@"fetch successful"); }] addOnFailureCallback:^(NSError * _Nonnull error) { NSLog(@"fetch failed"); }]; } - (void)showAllValue { NSDictionary *val = [[AGCRemoteConfig sharedInstance] getMergedAll]; _label.text = [val description]; } @end

3.Apply a parameter value upon the next launch.
Set the default value on the page that is displayed after the Mode 2 button is tapped. Call the apply method to pass the value of AGCConfigValues fetched last time and make it take effect. In this way, the latest parameter value can be applied without asynchronous waiting.

#import "SecondModeViewController.h" #import <AGConnectRemoteConfig/AGConnectRemoteConfig.h> @implementation SecondModeViewController - (void)viewDidLoad { [super viewDidLoad]; NSDictionary *defaultConfig = @{@"test1":@"value1", @"test2":@(2)}; [[AGCRemoteConfig sharedInstance] applyDefaults:defaultConfig]; AGCConfigValues *lastFetchedConfig = [[AGCRemoteConfig sharedInstance] loadLastFetched]; [[AGCRemoteConfig sharedInstance] apply:lastFetchedConfig]; [self showAllValue]; [[[[AGCRemoteConfig sharedInstance] fetch] addOnSuccessCallback:^(AGCConfigValues * _Nullable result) { NSLog(@"fetch successful"); }] addOnFailureCallback:^(NSError * _Nonnull error) { NSLog(@"fetch failed"); }]; } - (void)showAllValue { NSDictionary *val = [[AGCRemoteConfig sharedInstance] getMergedAll]; _label.text = [val description]; } @end

4.Call the clearAll method in the tap event of the Clear Data button to clear all parameter values, including default values and values on the cloud.

#import "ViewController.h" #import "FirstModeViewController.h" #import "SecondModeViewController.h" #import <AGConnectRemoteConfig/AGConnectRemoteConfig.h> @implementation ViewController ... - (IBAction)ClearDataButtonTapped:(id)sender { [[AGCRemoteConfig sharedInstance] clearAll]; } ... @end

1.Click Operation in the upper right corner of the card of the parameter test1, select Edit, change the default value, and click Save and Release.

Click the Xcode run button to run your app on a mobile phone or simulator. Launch your app and tap Mode 1. The modified values are displayed.

2.Tap Clear Data. All on-cloud parameter values are cleared. When you tap Mode 2 for the first time, the default values set in the app are displayed. Tap Mode 2 again. Pass the parameter values fetched last time to the apply method. The modified values are displayed.

Well done. You have successfully built an app that integrates Remote Configuration of AppGallery Connect and learned how to set parameter values in AppGallery Connect. You can also add different data types of parameter values in AppGallery Connect and view the data in your app.

For details about APIs related to Remote Configuration in AppGallery Connect, please refer to API Reference.
To download the sample code, please click the button below:

Download source code

Code copied