This Provider is designed to enable the use of OpenFeature in iOS, with Split as the backing feature flag & experimentation platform.
The Split OpenFeature Provider supports:
- iOS 14+
Below is a simple example that describes the instantiation of the Split Provider. Please see the OpenFeature Documentation for details on how to use the OpenFeature SDK.
Add the Split OpenFeature Provider dependency to your XCode project via Swift Package Manager.
.package(url: "https://github.com/splitio/split-openfeature-provider-swift", from: "1.1.0")The Split OpenFeature Provider requires an iOS Context and your Split SDK key. You must also provide an evaluation context with a targeting key when initializing the provider.
import Split
import Combine
import OpenFeature
import SplitProvider
Task {
var providerCancellable: AnyCancellable?
let provider = SplitProvider(key: "API_KEY")
// Setup events observer
providerCancellable = OpenFeatureAPI.shared.observe().sink { [weak self] event in
switch event {
case .ready:
print("Split Provider is ready")
case .error(let message):
print("Split Provider error:", message)
default:
break
}
}
// Setup OpenFeature
let context = ImmutableContext(targetingKey: "user_key")
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: context)
// Get a client and evaluate a flag
let client = OpenFeatureAPI.getClient()
let flagEvaluationResult = client.getBooleanValue("new-feature", false)
}The Split OpenFeature Provider requires a targeting key to be set in the evaluation context. This key identifies the user or entity for which you are evaluating feature flags.
let initialContext = ImmutableContext(targetingKey: "user-123")
await OpenFeatureAPI.setProviderAndWait(provider: provider, initialContext: initialContext)let newContext = ImmutableContext(targetingKey: "user-456")
await OpenFeatureAPI.setEvaluationContextAndWait(evaluationContext: newContext)let context = ImmutableContext(
targetingKey: "martin",
structure: ImmutableStructure(attributes: [
"email": Value.String(someValue),
"age": Value.Integer(30)
]))
await OpenFeatureAPI.shared.setEvaluationContextAndWait(evaluationContext: context)
let client = OpenFeatureAPI.getClient()
let result = client.getBooleanDetails("premium-feature", false, context)Set bucketingKey next to the targeting key to have Split bucket on a different key (Split key Key(matchingKey: targetingKey, bucketingKey: bucketingKey)):
let context = ImmutableContext(
targetingKey: "user-123",
structure: ImmutableStructure(attributes: ["bucketingKey": Value.string("account-42")]))
await OpenFeatureAPI.shared.setEvaluationContextAndWait(evaluationContext: context)bucketingKeyrequires a targeting key. At initialization and on a context change, a missing targeting key fails withTARGETING_KEY_MISSING. After a failed context change, OpenFeature returns evaluation defaults until a valid context is set.- The key is bound to the Split client: it applies to the initial context and to context changes, not to the invocation context of a single evaluation. Changing only
bucketingKeyswitches to a new Split client (the provider waits for its readiness), like changing the targeting key. Changing only other attributes keeps the same client. bucketingKeyis reserved: it is not forwarded to Split as an attribute. Only the exact namebucketingKeyis reserved; other spellings (BucketingKey,bucketing_key) are regular attributes.- Leading/trailing whitespace is trimmed. A blank or non-string value is ignored with a warning and the targeting key is used alone. Warnings are logged through
os_log(subsystemio.split.openfeature) and never include the value. - A value longer than 250 characters is rejected by the Split SDK. In the initial context and on later context changes, the provider logs a warning and every evaluation returns the default value with
FLAG_NOT_FOUND; it does not fall back to the targeting key.
The Split OpenFeature Provider emits events when the provider state changes. You can observe these events to react to provider readiness, configuration changes, or errors.
import Combine
let cancellable = OpenFeatureAPI.shared.observe().sink { event in
switch event {
case ProviderEvent.ready:
// ...
case ProviderEvent.stale:
// ...
case ProviderEvent.configurationChanges:
// ...
case ProviderEvent.contextChanged:
// ...
case ProviderEvent.error(let errorCode, let message):
// ...
default:
// ...
}
}Refer to the official Open Feature documentation to see the supported events: https://openfeature.dev/specification/types#provider-events
Please see Contributors Guide to find all you need to submit a Pull Request (PR).
Licensed under the Apache License, Version 2.0. See: Apache License.