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.0.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)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.