Skip to main content

Using the iOS SDK

appflags SwiftPM compatible GitHub Repo stars

iOS SDK information

SDK Initialization

It takes the AppFlags iOS SDK about 100ms to fetch the user's Feature Flags from the AppFlags edge servers.

You can use the AppFlagsClient immediately after creating it. The Feature Flags it returns will have default values until the SDK has initialized. See Default Values to learn more.

Automatic Updates

The AppFlags iOS SDK listens for updates to your Feature Flags. When you update Feature Flags in the AppFlags dashboard, the AppFlags client is immediately notified and updates with new Feature Flag values. See Feature Flag Value Updates to learn more.

Getting a Feature Flag

To get a Feature Flag, call the relevant getFlag function with the flag_key and a default_value.

let flag: AppFlag<Bool> = appFlagsClient.getBooleanFlag(flagKey: "flag_key", defaultValue: false)

The returned AppFlag object contains the current feature flag value in the value property:

val flagValue: Bool = flag.value

Default values

If the SDK has not initialized yet or the Feature Flag specified by the flag_key does not exist, then the returned AppFlag will contain the default value that you provided.

tip

If you are not 100% sure that the SDK has already initialized, you should subscribe to Feature Flag updates to get notified of the flag's actual value once the SDK has initialized. See Subscribe to Feature Flag Updates.

Variation types

There are getFlag functions for boolean, number, and string Feature Flags:

let booleanFlag: AppFlag<Bool> = appFlagsClient.getBooleanFlag(flagKey: "boolean_flag", defaultValue:false)

let numberFlag: AppFlag<Double> = appFlagsClient.getNumberFlag(flagKey: "number_flag ", defaultValue: 0.0)

let stringFlag: AppFlag<String> = appFlagsClient.getStringFlag(flagKey: "string_flag", defaultValue: "default")

Feature Flag Value Updates

The value property in AppFlag will automatically update in the following situations:

  • The SDK finishes initializing from the AppFlags servers
  • The Feature Flag is modified in the AppFlags dashboard
  • The current user has been changed

Your code will likely not read the new value automatically. If you wish to get notified when the Feature Flag updates, then Subscribe to Feature Flag Updates.

Subscribe to Feature Flag Updates

To subscribe to Realtime Updates on a Feature Flag, add an onUpdate callback:

_ = flag.onUpdate { (value: Bool) in
... = value
}
info

In the example above, the value in the existing flag object has updated, but we provide the updated value in the callback for convenience.

Update the User

The user of your application may change as they login, logout, or update their information.

To update the user in the AppFlags SDK, construct a new AppFlagsUser and call updateUser:

let newUser = AppFlagsUser(key: "new-user-key")
appFlagsClient.updateUser(user: newUser)

The AppFlags SDK will fetch updated Feature Flags and update any existing AppFlag instances if necessary.

Example Repository

For a full example of using AppFlags in an iOS Swift application, see https://github.com/AppFlags/example-ios-swift.

tip

For ease of use, we suggest creating an ObservableObject that contains your AppFlags Feature Flags. The iOS Swift example application contains an example of this.