Objective-C Core Interfaces
-
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 context (LDContext) 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.
Objc Classes
The SDK creates an Objective-C native style API by wrapping Swift specific classes, properties, and methods into Objective-C wrapper classes prefixed by
Objc
. By defining Objective-C specific names, client apps written in Objective-C can use a native coding style, including using familiar LaunchDarkly SDK names likeLDClient
,LDConfig
, andLDContext
. Objective-C developers should refer to the Objc documentation by following the Objc specific links following type, property, and method names.Usage
Startup
- To customize, configure a LDConfig (
ObjcLDConfig
) and LDContext (ObjcLDContxt
). Both give you additional control over the feature flags delivered to the LDClient. SeeObjcLDConfig
&ObjcLDContext
for more details. - The mobileKey set into the
LDConfig
comes from your LaunchDarkly Account settings (on the left, at the bottom). If you have multiple projects be sure to choose the correct Mobile key. - Call
[ObjcLDClient startWithConfig: context: completion:]
(ObjcLDClient.startWithConfig(_:config:context:completion:)
) - If you do not pass in a LDContext, LDCLient will create a default for you.
- The optional completion closure allows the LDClient to notify your app when it has gone online.
- Because the LDClient is a singleton, you do not have to keep a reference to it in your code.
Getting Feature Flags
Once the LDClient has started, it makes your feature flags available using the
variation
andvariationDetail
methods. Avariation
is a specific flag value. For example, a boolean feature flag has 2 variations,YES
andNO
. You can create feature flags with more than 2 variations using other feature flag types. SeeLDValue
for the available types.BOOL boolFlag = [ldClientInstance boolVariationForKey:@"my-bool-flag" defaultValue:NO];
If you need to know more information about why a given value is returned, the typed
variationDetail
methods return anLD<T>EvaluationDetail
with an detail about the evaluation.LDBoolEvaluationDetail *boolVariationDetail = [ldClientInstance boolVariationDetail:@"my-bool-flag" defaultValue:NO]; BOOL boolFlagValue = boolVariationDetail.value; NSInteger boolFlagVariation = boolVariationDetail.variationIndex NSDictionary boolFlagReason = boolVariationValue.reason;
See the typed
-[LDCLient variationForKey: defaultValue:]
or-[LDClient variationDetailForKey: defaultValue:]
methods in the section Feature Flag values for details.Observing Feature Flags
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 setup notifications based on when a specific flag changes, when any flag in a collection changes, or when a flag doesn’t change. The flag change listener may be invoked multiple times per invocation of LDClient.identify as the SDK fetches up to date flag data from multiple sources (e.g. local cache, cloud services). In certain error cases, the SDK may not be able to retrieve flag data during an identify (e.g. no network connectivity). In those cases, the flag change listener may not be invoked.__weak typeof(self) weakSelf = self; [ldClientInstance observeBool:@"my-bool-flag" owner:self handler:^(LDBoolChangedFlag *changedFlag) { __strong typeof(weakSelf) strongSelf = weakSelf; [strongSelf updateFlagWithKey:@"my-bool-flag" changedFlag:changedFlag]; }];
The
See morechangedFlag
passed in to the block contains the old and new value. See the typedLDChangedFlag
classes in the Obj-C Changed Flags.Declaration
Swift
@objc(LDClient) public final class ObjcLDClient : NSObject
- To customize, configure a LDConfig (
-
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
See moreconfig
from LDClient (ObjcLDClient
), adjusting the values, and setting it back into the LDClient (ObjcLDClient
).Declaration
Swift
@objc(LDConfig) public final class ObjcLDConfig : NSObject
-
Undocumented
See moreDeclaration
Swift
@objc(Reference) public final class ObjcLDReference : NSObject
-
LDContext is a collection of attributes that can be referenced in flag evaluations and analytics events.
To create an LDContext of a single kind, such as a user, you may use
LDContextBuilder
.To create an LDContext with multiple kinds, use
See moreLDMultiContextBuilder
.Declaration
Swift
@objc(LDContext) public final class ObjcLDContext : NSObject
-
Collects the elements of a feature flag that changed as a result of a
clientstream
update or feature flag request. The SDK will pass a typed ObjcLDChangedFlag or a collection of ObjcLDChangedFlags into feature flag observer blocks. This is the base type for the typed ObjcLDChangedFlags passed into observer blocks. The client app will have to convert the ObjcLDChangedFlag into the expected typed ObjcLDChangedFlag type.See the typed
See moreObjcLDClient
observeWithKey:owner:handler:, observeWithKeys:owner:handler:, and observeAllWithOwner:handler: for more details.Declaration
Swift
@objc(LDChangedFlag) public class ObjcLDChangedFlag : NSObject