Core Classes

  • The LDClient is the heart of the SDK, providing client apps running iOS, watchOS, macOS, or tvOS access to LaunchDarkly services. This singleton provides the ability to set a configuration (LDConfig) that controls how the LDClient talks to LaunchDarkly servers, and a user (LDUser) that provides finer control on the feature flag values delivered to LDClient. Once the LDClient has started, it connects to LaunchDarkly’s servers to get the feature flag values you set in the Dashboard.

    Usage

    Startup

    1. To customize, configure a LDConfig and LDUser. The config is required, the user is optional. Both give you additional control over the feature flags delivered to the LDClient. See LDConfig & LDUser for more details.
      • The mobileKey set into the LDConfig comes from your LaunchDarkly Account settings. If you have multiple projects be sure to choose the correct Mobile key.
    2. Call LDClient.start(config: user: completion:)
      • If you do not pass in a LDUser, LDClient will create a default for you.
      • The optional completion closure allows the LDClient to notify your app when it received flag values.
    3. Because LDClient instances are stored statically, you do not have to keep a reference to it in your code. Get the primary instances with LDClient.get()

    Getting Feature Flags

    Once the LDClient has started, it makes your feature flags available using the variation and variationDetail methods. A variation is a specific flag value. For example a boolean feature flag has 2 variations, true and false. You can create feature flags with more than 2 variations using other feature flag types.

     let boolFlag = LDClient.get()?.variation(forKey: "my-bool-flag", defaultValue: false)
    

    If you need to know more information about why a given value is returned, use variationDetail.

    See variation(forKey: defaultValue:) or variationDetail(forKey: defaultValue:) for details

    Observing Feature Flags

    You might need to know when a feature flag value changes. This is not required, you can check the flag’s value when you need it.

    If you want to know when a feature flag value changes, you can check the flag’s value. You can also use one of several observe methods to have the LDClient notify you when a change occurs. There are several options–you can set up notificiations based on when a specific flag changes, when any flag in a collection changes, or when a flag doesn’t change.

     LDClient.get()?.observe("flag-key", owner: self, observer: { [weak self] (changedFlag) in
        self?.updateFlag(key: "flag-key", changedFlag: changedFlag)
     }
    

    The changedFlag passed in to the closure contains the old and new value of the flag.

    See more

    Declaration

    Swift

    public class LDClient
  • Use LDConfig to configure the LDClient. When initialized, a LDConfig contains the default values which can be changed as needed.

    The client app can change the LDConfig by getting the config from LDClient, adjusting the values, and setting it back into the LDClient.

    See more

    Declaration

    Swift

    public struct LDConfig
    extension LDConfig: Equatable
  • LDUser allows clients to collect information about users in order to refine the feature flag values sent to the SDK. For example, the client app may launch with the SDK defined anonymous user. As the user works with the client app, information may be collected as needed and sent to LaunchDarkly. The client app controls the information collected, which LaunchDarkly does not use except as the client directs to refine feature flags. Client apps should follow Apple’s Privacy Policy when collecting user information. The SDK caches last known feature flags for use on app startup to provide continuity with the last app run. Provided the LDClient is online and can establish a connection with LaunchDarkly servers, cached information will only be used a very short time. Once the latest feature flags arrive at the SDK, the SDK no longer uses cached feature flags. The SDK retains feature flags on the last 5 client defined users. The SDK will retain feature flags until they are overwritten by a different user’s feature flags, or until the user removes the app from the device. The SDK does not cache user information collected, except for the user key. The user key is used to identify the cached feature flags for that user. Client app developers should use caution not to use sensitive user information as the user-key.

    See more

    Declaration

    Swift

    public struct LDUser
    extension LDUser: Equatable
  • An object returned by the SDK’s variationDetail methods, combining the result of a flag evaluation with an explanation of how it is calculated.

    See more

    Declaration

    Swift

    public final class LDEvaluationDetail<T>