splitio/split-openfeature-provider-swift
This Provider is designed to enable the use of OpenFeature in iOS, with Split as the backing feature flag & experimentation platform.
Overview
This Provider is designed to enable the use of OpenFeature in iOS, with Split as the backing feature flag & experimentation platform.
Compatibility
The Split OpenFeature Provider supports:
- iOS 14+
Getting started
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.
Installation
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")Usage
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)
}Evaluation Context
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.
Setting a targeting key during initialization:
let initialContext = ImmutableContext(targetingKey: "user-123")
await OpenFeatureAPI.setProviderAndWait(provider: provider, initialContext: initialContext)Changing the targeting key at runtime:
let newContext = ImmutableContext(targetingKey: "user-456")
await OpenFeatureAPI.setEvaluationContextAndWait(evaluationContext: newContext)Using attributes for targeting:
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)Observing Provider Events
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
Contributing
Please see Contributors Guide to find all you need to submit a Pull Request (PR).
License
Licensed under the Apache License, Version 2.0. See: Apache License.
Package Metadata
Repository: splitio/split-openfeature-provider-swift
Default branch: main
README: README.md