OpenFeature is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool.
Standardizing feature flags unifies tools and vendors behind a common interface which avoids vendor lock-in at the code level. Additionally, it offers a framework for building extensions and integrations and allows providers to focus on their unique value proposition.
- The minimum iOS version supported is:
iOS 14
.
Note that this library is intended to be used in a mobile context, and has not been evaluated for use in other type of applications (e.g. server applications, macOS, tvOS, watchOS, etc.).
You have two options, both start from File > Add Packages... in the code menu.
First, ensure you have your GitHub account added as an option (+ > Add Source Control Account...). You will need to create a Personal Access Token with the permissions defined in the Xcode interface.
- Add as a remote repository
- Search for
git@github.com:open-feature/swift-sdk.git
and click "Add Package"
- Search for
- Clone the repository locally
- Clone locally using your preferred method
- Use the "Add Local..." button to select the local folder
Note: Option 2 is only recommended if you are making changes to the client SDK.
If you manage dependencies through SPM, in the dependencies section of Package.swift add:
.package(url: "git@github.com:open-feature/swift-sdk.git", from: "0.0.2")
and in the target dependencies section add:
.product(name: "OpenFeature", package: "openfeature-swift-sdk"),
- Support for various backend providers
- Easy integration and extension via hooks
- Bool, string, numeric, and object flag types
- Context-aware evaluation
import OpenFeature
// Configure your custom `FeatureProvider` and pass it to OpenFeatureAPI
let customProvider = MyCustomProvider()
OpenFeatureAPI.shared.setProvider(provider: customProvider)
// Configure your evaluation context and pass it to OpenFeatureAPI
let ctx = MutableContext(
targetingKey: userId,
structure: MutableStructure(attributes: ["product": Value.string(productId)]))
OpenFeatureAPI.shared.setEvaluationContext(evaluationContext: ctx)
// Get client from OpenFeatureAPI and evaluate your flags
let client = OpenFeatureAPI.shared.getClient()
let flagValue = client.getBooleanValue(key: "boolFlag", defaultValue: false)
Setting a new provider or setting a new evaluation context are synchronous operations. The provider might execute I/O operations as part of these method calls (e.g. fetching flag evaluations from the backend and store them in a local cache). It's advised to not interact with the OpenFeature client until the relevant event has been emitted (see events below).
Please refer to our documentation on static-context APIs for further information on how these APIs are structured for the use-case of mobile clients.
Events allow you to react to state changes in the provider or underlying flag management system, such as flag definition changes, provider readiness, or error conditions.
Initialization events (PROVIDER_READY
on success, PROVIDER_ERROR
on failure) are emitted for every provider.
Some providers support additional events, such as PROVIDER_CONFIGURATION_CHANGED
.
Please refer to the documentation of the provider you're using to see what events are supported and when they are emitted.
To register a handler for API level provider events, use the OpenFeatureAPI
addHandler(observer:selector:event:)
function. Event handlers can also be removed using the equivalent removeHandler
function. Client
s also provide add and remove functions for listening to that specific client. A ProviderEvent
enum is defined to ease subscription.
Events can contain extra information in the userInfo
dictionary of the notification. All events will contain a reference to the provider that emitted the event (under the providerEventDetailsKeyProvider
key). Error events will also provide a reference to the underlying error (under the providerEventDetailsKeyError
key). Finally, client specific events will contain a reference to the client (under the providerEventDetailsKeyClient
key)
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a new repository or included in the existing contrib repository available under the OpenFeature organization. Finally, you’ll then need to write the provider itself. This can be accomplished by implementing the FeatureProvider
protocol exported by the OpenFeature SDK.
import OpenFeature
final class CustomProvider: FeatureProvider {
var hooks: [any Hook] = []
var metadata: ProviderMetadata = CustomMetadata()
func initialize(initialContext: EvaluationContext?) async {
// add context-aware provider initialisation
}
func onContextSet(oldContext: EvaluationContext?, newContext: EvaluationContext) async {
// add necessary changes on context change
}
func getBooleanEvaluation(
key: String,
defaultValue: Bool,
context: EvaluationContext?
) throws -> ProviderEvaluation<Bool> {
// resolve a boolean flag value
}
func getStringEvaluation(
key: String,
defaultValue: String,
context: EvaluationContext?
) throws -> ProviderEvaluation<String> {
// resolve a string flag value
}
func getIntegerEvaluation(
key: String,
defaultValue: Int64,
context: EvaluationContext?
) throws -> ProviderEvaluation<Int64> {
// resolve an integer flag value
}
func getDoubleEvaluation(
key: String,
defaultValue: Double,
context: EvaluationContext?
) throws -> ProviderEvaluation<Double> {
// resolve a double flag value
}
func getObjectEvaluation(
key: String,
defaultValue: Value,
context: EvaluationContext?
) throws -> ProviderEvaluation<Value> {
// resolve an object flag value
}
}
- Give this repo a ⭐️!
- Follow us on social media:
- Twitter: @openfeature
- LinkedIn: OpenFeature
- Join us on Slack
- For more check out our community page
Interested in contributing? Great, we'd love your help! To get started, take a look at the CONTRIBUTING guide.
Made with contrib.rocks.