Quick-start
Installation
Refer to the installation page for installation steps.
Getting started
Below is a simple example that describes the instantiation of the ABTasty Provider. Please see the OpenFeature Documentation for details on how to use the OpenFeature SDK.
Add the following to your Package.swift:
dependencies: [
.package(url: "https://github.com/flagship-io/openfeature-provider-iOS.git", from: "1.0.0")
]
Import the required modules:
import ABTastyOpenfeature_iOS
import OpenFeature
Register the ABTastyProvider with OpenFeature
Init
and Set
provider with context
// Create a context with an "openUserId"
let ctx = MutableContext(
targetingKey: "openUserId",
structure: MutableStructure(attributes: ["isQA": Value.boolean(true),
"city": Value.string("FR"),
"hasConsented": Value.boolean(true),
"ctx1": Value.boolean(false),
"ctx2": Value.integer(125),
"ctx3": Value.double(12.0)])
Task {
// Create ABTasty provider
let provider = ABTastyProvider(envId: "envId", apiKey: "apiKey", configurator: FSConfigBuilder().build())
// Set provider through OpenFeature API
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: ctx)
}
Flag Evaluation (Read Flag)
After init
and set
provider with the apropriate context
use the client instance to get the evaluation flag
// Update UIs on the main thread
DispatchQueue.main.async {
// get a bool flag value
let client = OpenFeatureAPI.shared.getClient()
// Retreive the String flag for 'btnTitle'key
let btnTitle = client.getStringValue(key: "btnTitle", defaultValue: "default")
// Retreive the Integer flag for 'intValue'key
let intValue = client.getIntegerValue(key: "intValue", defaultValue: 0)
// Update UIs
}
Context Updates
On visitor context changed use setEvaluationContextAndWait
function before read flag value
// New context to inject
let updatedContext = MutableContext(
targetingKey: "openUserId",
structure: MutableStructure(attributes: ["isQA": Value.boolean(false)]))
Task {
await OpenFeatureAPI.shared.setEvaluationContextAndWait(evaluationContext: updatedContext)
// Refresh the values after context update
DispatchQueue.main.async {
let client = OpenFeatureAPI.shared.getClient()
// get String value for 'btnTitle' key flag
let btnTitle = client.getStringValue(key: "btnTitle", defaultValue: "default")
// get Integer value for 'intValue' key flag
let intValue = client.getIntegerValue(key: "intValue", defaultValue: 0)
// Update UIs
}
}
Last updated
Was this helpful?