iOS Remote Config

Learn how to set up Remote Config for your iOS application.

Overview

With the CleverTap iOS SDK, you can create variables on your app that take on new values from the CleverTap dashboard. Using variables in the CleverTap dashboard allows you to roll out changes without pushing an update through the Apple App Store. These can also be used in A/B tests to test features for only a percentage of your users.

You can define variables using the CleverTap iOS SDK. When you define a variable in your code, you can sync them to the CleverTap Dashboard via the provided SDK methods.

📘

SDK Prerequisite

Before you proceed, check that you are using iOS SDK v5.0.0 or higher to use the Remote Config feature.

Supported Variable Types

Currently, CleverTap SDK supports the following variable types:

  • String
  • boolean
  • int
  • float
  • double
  • short
  • long
  • Number

Define Variables

Variables can be defined using a shared or custom CleverTap instance. The Variable is defined using the defineVar method, which returns an instance of a CTVar variable. You must provide the name and default value of the variable.

// Primitive types
let var_string = CleverTap.sharedInstance()?.defineVar(name: "var_string", string: "hello, world")
let var_int = CleverTap.sharedInstance()?.defineVar(name: "var_int", integer: 10)
let var_bool = CleverTap.sharedInstance()?.defineVar(name: "var_bool", boolean: true)
let var_float = CleverTap.sharedInstance()?.defineVar(name: "var_float", float: 6.0)
let var_double = CleverTap.sharedInstance()?.defineVar(name: "var_double", double: 60.999)
let var_short = CleverTap.sharedInstance()?.defineVar(name: "var_short", short: 1)
let var_number = CleverTap.sharedInstance()?.defineVar(name: "var_number", number: NSNumber(value: 32))
let var_long = CleverTap.sharedInstance()?.defineVar(name: "var_long", long: 64)
// Dictionary
let var_dict = CleverTap.sharedInstance()?.defineVar(name: "var_dict", dictionary: [
        "nested_string": "hello, nested",
        "nested_double": 10.5
    ])

let var_dict_nested = CleverTap.sharedInstance()?.defineVar(name: "var_dict_complex", dictionary: [
        "nested_int": 1,
        "nested_string": "hello, world",
        "nested_map": [
            "nested_map_int": 15,
            "nested_map_string": "hello, nested map",
        ]
    ])



#import <CleverTapSDK/CleverTap+CTVar.h>

// Primitive types
    CTVar *var_string = [[CleverTap sharedInstance] defineVar:@"var_string" withString:@"hello, world"];
    CTVar *var_int = [[CleverTap sharedInstance] defineVar:@"var_int" withInt:10];
    CTVar *var_bool = [[CleverTap sharedInstance] defineVar:@"var_bool" withBool:YES];
    CTVar *var_float = [[CleverTap sharedInstance] defineVar:@"var_float" withFloat:6.0];
    CTVar *var_double = [[CleverTap sharedInstance] defineVar:@"var_double" withDouble:60.999];
    CTVar *var_short = [[CleverTap sharedInstance] defineVar:@"var_short" withShort:1];
    CTVar *var_number = [[CleverTap sharedInstance] defineVar:@"var_number" withNumber:[[NSNumber alloc] initWithInt:32]];
    CTVar *var_long = [[CleverTap sharedInstance] defineVar:@"var_long" withLong:64];
    // Dictionary
    CTVar *var_dict = [[CleverTap sharedInstance] defineVar:@"var_dict" withDictionary:@{
        @"nested_string": @"hello, nested",
        @"nested_double": @10.5
    }];
  CTVar *var_dict_nested = [[CleverTap sharedInstance] defineVar:@"var_dict_complex" withDictionary:@{
            @"nested_int": @1,
            @"nested_string": @"hello, world",
            @"nested_map": @{
                @"nested_map_int": @15,
                @"nested_map_string": @"hello, nested map",
            }
    }];

Setup Callbacks

CleverTap iOS SDK provides several callbacks for the developer to receive feedback from the SDK. You can use them as per your requirement, using all of them is not mandatory. They are as follows:

  • Status of fetch variables request
  • onVariablesChanged
  • onceVariablesChanged
  • onValueChanged
  • Variables Delegate

Status of Variables Fetch Request

This method provides a boolean flag to ensure that the variables are successfully fetched from the server.

CleverTap.sharedInstance()?.fetchVariables({ success in
    print(success)
})
#import <CleverTapSDK/CleverTap+CTVar.h>

[[CleverTap sharedInstance] fetchVariables:^(BOOL success) {
        
    }];

onVariablesChanged

This callback is invoked when variables are initialized with values fetched from the server. It is called each time new values are fetched.

let var_string = CleverTap.sharedInstance()?.defineVar(name: "myString", string: "hello,world")
CleverTap.sharedInstance()?.onVariablesChanged {
    print("CleverTap.onVariablesChanged: \(var_string?.value ?? "")")
}


#import <CleverTapSDK/CleverTap+CTVar.h>

CTVar *var_string = [[CleverTap sharedInstance] defineVar:@"var_string" withString:@"hello, world"];
    [[CleverTap sharedInstance] onVariablesChanged:^{
        NSLog(@"CleverTap.onVariablesChanged: %@", [var_string value]);
    }];

onceVariablesChanged

This callback is invoked when variables are initialized with values fetched from the server. It is called only once.

let var_string = CleverTap.sharedInstance()?.defineVar(name: "myString", string: "hello,world")
CleverTap.sharedInstance()?.onceVariablesChanged {
    print("CleverTap.onceVariablesChanged: \(var_string?.value ?? "")")
}

#import <CleverTapSDK/CleverTap+CTVar.h>

CTVar *var_string = [[CleverTap sharedInstance] defineVar:@"var_string" withString:@"hello, world"];


    [[CleverTap sharedInstance] onceVariablesChanged:^{
        // Executed only once
        NSLog(@"CleverTap.onceVariablesChanged: %@", [var_string value]);
    }];

onValueChanged

This callback is invoked when the value of the variable changes.

let var_string = CleverTap.sharedInstance()?.defineVar(name: "myString", string: "hello,world")
var_string?.onValueChanged {
    print("var_string.onValueChanged: \(var_string?.value ?? "")")
}

#import <CleverTapSDK/CleverTap+CTVar.h>

CTVar *var_string = [[CleverTap sharedInstance] defineVar:@"var_string" withString:@"hello, world"];
    [var_string onValueChanged:^{
        NSLog(@"var_string.onValueChanged: %@", [var_string value]);
    }];

Variables Delegate

The VarDelegate method is implemented to be invoked when the variable value is changed.

@objc class VarDelegateImpl: NSObject, VarDelegate {
    func valueDidChange(_ variable: CleverTapSDK.Var) {
        print("CleverTap \(String(describing: variable.name)):valueDidChange to: \(variable.value!)")
    }
}

var_string?.setDelegate(self)
#import <CleverTapSDK/CleverTap+CTVar.h>

@interface CTVarDelegateImpl : NSObject <CTVarDelegate>
@end


@implementation CTVarDelegateImpl
- (void)valueDidChange:(CTVar *)variable {
// valueDidChange
}
@end

CTVarDelegateImpl *del = [[CTVarDelegateImpl alloc] init];
[var_string setDelegate:del];

Sync Defined Variables

After defining your variables in the code, you must send/sync variables to the server. To do so, the app must be in DEBUG mode and mark a particular CleverTap user profile as a test profile from the CleverTap dashboard. Learn how to mark a profile as Test Profile

After marking the profile as a test profile, you must sync the app variables in DEBUG mode:

// 1. Define CleverTap variables 
// …
// 2. Add variables/values changed callbacks
// …

// 3. Sync CleverTap Variables from DEBUG mode/builds
CleverTap.sharedInstance()?.syncVariables();
// 1. Define CleverTap variables 
// …
// 2. Add variables/values changed callbacks
// …

// 3. Sync CleverTap Variables from DEBUG mode/builds
[[CleverTap sharedInstance] syncVariables];

📘

Key Points to Remember

  • In a scenario where there is already a draft created by another user profile in the dashboard, the sync call will fail to avoid overriding important changes made by someone else. In this case, Publish or Dismiss the existing draft before you proceed with syncing variables again. However, you can override a draft you created via the sync method previously to optimize the integration experience.
  • You can receive the following Logcat logs from the CleverTap SDK:
    • Variables synced successfully.
    • Unauthorized access from a non-test profile. To address this, mark the profile as a test profile from the CleverTap dashboard.

Fetch Variables During a Session

You can fetch the updated values for your CleverTap variables from the server during a session. If variables have changed, the appropriate callbacks will be fired. The provided callback provides a boolean flag that indicates if the fetch call was successful. The callback is fired regardless of whether the variables have changed or not.

CleverTap.sharedInstance()?.fetchVariables({ success in
    print(success)
})
  [[CleverTap sharedInstance] fetchVariables:^(BOOL success) {
        
    }];

Use Fetched Variables Values

This process involves the following two major steps:

  1. Fetch variable values.
  2. Access variable values.

Fetch Variable Values

Variables are updated automatically when server values are received. If you want to receive feedback when a specific variable is updated, use the individual callback:

variable?.onValueChanged {
    print("variable.onValueChanged: \(variable?.value ?? "")")
}
 [variable onValueChanged:^{
        NSLog(@"variable.onValueChanged: %@", [variable value]);
    }];

Access Variable Values

You can access these fetched values in the following three ways:

From Var instance

You can use several methods on the Var instance as shown in the following code:

variable?.defaultValue // returns default value
variable?.value // returns current value
variable?.numberValue // returns value as NSNumber if applicable
variable?.stringValue // returns value as String
variable.defaultValue; // returns default value
variable.value; // returns current value
variable.numberValue; // returns value as NSNumber if applicable
variable.stringValue; // returns value as String

Using CleverTap Instance method

You can use the CleverTap instance method to get the current value of a variable. If the variable is nonexistent, the method returns null:

CleverTap.sharedInstance()?.getVariableValue("variable name")
[[CleverTap sharedInstance]getVariableValue:@"variable name"];