Launch Darkly

Tested with Launch Darkly version 9.12.0 (iOS)

We assume you already have Appfigurate Library and Launch Darkly integrated into your app with the following example remote properties created in the Launch Darkly console:

Add remote properties into your Configuration subclass

Update your configuration subclass to include your remote configuration properties. Provide default values for the properties in the overridden reset method. See also Supported property types.

Swift Configuration example

import Foundation
import AppfigurateLibrary

@objcMembers class MyConfiguration: APLConfiguration {
...
    @RemoteBoolProperty(remoteKey: "alwaysDarkMode", description: "Force dark mode to be always set")
    var alwaysDarkMode: Bool

    @RemoteStringPropertyEdit(remoteKey: "appTitle", description: "Title of application")
    var appTitle: String

    @RemoteIntPropertyEdit(remoteKey: "bookingDuration", description: "Duration (days) for reservation bookings")
    var bookingDuration: Int

    @RemoteDoublePropertyEdit(remoteKey: "fontSize", description: "Size of font throughout app")
    var fontSize: Double
...
    override func reset() {
        alwaysDarkMode = false
        appTitle = "Holiday finder"
        bookingDuration = 30
        fontSize = 13.0
    }
...
}

Provide remote configuration values to Appfigurate Library when requested

Appfigurate Library needs to be able to read the current remote flags from Launch Darkly. Copy and paste the following code into your app.

Swift example

APLFetchRemoteConfiguration { propertyKey, propertyType, defaultValue in
    if propertyType == .bool {
        return NSNumber(value: (LDClient.get()!.boolVariation(forKey: propertyKey, defaultValue: (defaultValue as! NSNumber).boolValue)))
    } else if propertyType == .int {
        return NSNumber(value: (LDClient.get()!.intVariation(forKey: propertyKey, defaultValue: (defaultValue as! NSNumber).intValue)))
    } else if propertyType == .double {
        return NSNumber(value: (LDClient.get()!.doubleVariation(forKey: propertyKey, defaultValue: (defaultValue as! NSNumber).doubleValue)))
    } else { // .string 
        return NSString(string: LDClient.get()!.stringVariation(forKey: propertyKey, defaultValue: (defaultValue as! String)))
    }
}

Notify Appfigurate Library when Launch Darkly has received flags

We need to tell Appfigurate Library that Launch Darkly has received flags, so that it can keep your Configuration subclasses remote properties in sync.

Add a call to APLFlushRemoteConfiguration in any existing Launch Darkly start(config, startWaitSeconds, completion) and observeAll(owner, handler) blocks.

Swift example

LDClient.start(config: config, startWaitSeconds: 5.0) { timedOut in
    ...
    APLFlushRemoteConfiguration() // add this line
}
...
LDClient.get()?.observeAll(owner: self) { keys in
    ...
    APLFlushRemoteConfiguration() // add this line
}

Best practice and usage

Replace all calls to Launch Darkly boolVariation(forKey:defaultValue:), intVariation(forKey:defaultValue:), doubleVariation(forKey:defaultValue), stringVariation(forKey:defaultValue:)

if (LDClient.get()!.boolVariation(forKey: "alwaysDarkMode", defaultValue: false)) {
    ...

with the following:

if ((APLConfiguration.shared() as! MyConfiguration).alwaysDarkMode) {
    ...

Last updated