|
| 1 | +--- |
| 2 | +sidebar_position: 20 |
| 3 | +title: Swift |
| 4 | +description: How to use the OpenFeature Swift SDK for your iOS/tvOS/macOS application |
| 5 | +--- |
| 6 | +import Tabs from '@theme/Tabs'; |
| 7 | +import TabItem from '@theme/TabItem'; |
| 8 | + |
| 9 | +# Swift SDK |
| 10 | +[](https://github.com/go-feature-flag/openfeature-swift-provider) |
| 11 | + |
| 12 | +In conjuction with the [OpenFeature SDK](https://openfeature.dev/docs/reference/concepts/provider) you will be able to evaluate your feature flags in your **iOS**/**tvOS**/**macOS** applications. |
| 13 | + |
| 14 | +For documentation related to flags management in GO Feasture Flag, refer to the [GO Feature Flag documentation website](https://gofeatureflag.org/docs). |
| 15 | + |
| 16 | +### Functionalities: |
| 17 | +- Managed the integration of the OpenFeature Swift SDK and GO Feature Flag relay-proxy. |
| 18 | +- Prefetch and cache flag evaluations in order to give the flag value in a efficient way. |
| 19 | +- Automatic configuration changes polling, to be informed as soon as a flag configuration has changed. |
| 20 | +- Automatic data collection about which flags have been accessed by the application |
| 21 | + |
| 22 | +## Dependency Setup |
| 23 | +### Swift Package Manager |
| 24 | + |
| 25 | +In the dependencies section of Package.swift add: |
| 26 | +```swift |
| 27 | +.package(url: "https://github.com/go-feature-flag/openfeature-swift-provider.git", from: "0.1.0") |
| 28 | +``` |
| 29 | + |
| 30 | +and in the target dependencies section add: |
| 31 | +```swift |
| 32 | +.product(name: "GOFeatureFlag", package: "openfeature-swift-provider"), |
| 33 | +``` |
| 34 | + |
| 35 | +### Xcode Dependencies |
| 36 | + |
| 37 | +You have two options, both start from File > Add Packages... in the code menu. |
| 38 | + |
| 39 | +First, ensure you have your GitHub account added as an option (`+ > Add Source Control Account...`). You will need to create a [Personal Access Token](https://github.com/settings/tokens) with the permissions defined in the Xcode interface. |
| 40 | + |
| 41 | +1. Add as a remote repository |
| 42 | + * Search for `[email protected]:go-feature-flag/openfeature-swift-provider.git` and click "Add Package" |
| 43 | +2. Clone the repository locally |
| 44 | + * Clone locally using your preferred method |
| 45 | + * Use the "Add Local..." button to select the local folder |
| 46 | + |
| 47 | +**Note:** Option 2 is only recommended if you are making changes to the SDK. You will also need to add the relevant OpenFeature SDK dependency manually. |
| 48 | + |
| 49 | +## Getting started |
| 50 | + |
| 51 | +### Initialize the provider |
| 52 | + |
| 53 | +GO Feature Flag provider needs to be created and then set in the global OpenFeatureAPI. |
| 54 | + |
| 55 | +The only required option to create a `GoFeatureFlagProvider` is the URL to your GO Feature Flag relay-proxy instance. |
| 56 | + |
| 57 | +```swift |
| 58 | +import GOFeatureFlag |
| 59 | +import OpenFeature |
| 60 | + |
| 61 | + |
| 62 | +let options = GoFeatureFlagProviderOptions(endpoint: "https://your_domain.io") |
| 63 | +let provider = GoFeatureFlagProvider(options: options) |
| 64 | + |
| 65 | +let evaluationContext = MutableContext(targetingKey: "myTargetingKey", structure: MutableStructure()) |
| 66 | +OpenFeatureAPI.shared.setProvider(provider: provider, initialContext: evaluationContext) |
| 67 | +``` |
| 68 | + |
| 69 | +The evaluation context is the way for the client to specify contextual data that GO Feature Flag uses to evaluate the feature flags, it allows to define rules on the flag. |
| 70 | + |
| 71 | +The `targetingKey` is mandatory for GO Feature Flag in order to evaluate the feature flag, it could be the id of a user, a session ID or anything you find relevent to use as identifier during the evaluation. |
| 72 | + |
| 73 | +The `setProvider()` function is synchronous and returns immediately, however this does not mean that the provider is ready to be used. An asynchronous network request to the GO Feature Flag backend to fetch all the flags configured for your application must be completed by the provider first. The provider will then emit a `READY` event indicating you can start resolving flags. |
| 74 | + |
| 75 | +If you prefer to wait until the fetch is done you can use the `async/await` compatible API available for waiting the Provider to become ready: |
| 76 | + |
| 77 | +```swift |
| 78 | +await OpenFeatureAPI.shared.setProviderAndWait(provider: provider) |
| 79 | +``` |
| 80 | + |
| 81 | +### Update the Evaluation Context |
| 82 | + |
| 83 | +During the usage of your application it may appears that the `EvaluationContext` should be updated. For example if a not logged in user, authentify himself you will probably have to update the evaluation context. |
| 84 | + |
| 85 | +```swift |
| 86 | +let ctx = MutableContext(targetingKey: "myNewTargetingKey", structure: MutableStructure()) |
| 87 | +OpenFeatureAPI.shared.setEvaluationContext(evaluationContext: ctx) |
| 88 | +``` |
| 89 | + |
| 90 | +`setEvaluationContext()` is a synchronous function similar to `setProvider()` and will fetch the new version of the feature flags based on this new `EvaluationContext`. |
| 91 | + |
| 92 | +### Evaluate a feature flag |
| 93 | +The client is used to retrieve values for the current `EvaluationContext`. For example, retrieving a boolean value for the flag **"my-flag"**: |
| 94 | + |
| 95 | +```swift |
| 96 | +let client = OpenFeatureAPI.shared.getClient() |
| 97 | +let result = client.getBooleanValue(key: "my-flag", defaultValue: false) |
| 98 | +``` |
| 99 | + |
| 100 | +GO Feature Flag supports different all OpenFeature supported types of feature flags, it means that you can use all the accessor directly |
| 101 | +```swift |
| 102 | +// Bool |
| 103 | +client.getBooleanValue(key: "my-flag", defaultValue: false) |
| 104 | + |
| 105 | +// String |
| 106 | +client.getStringValue(key: "my-flag", defaultValue: "default") |
| 107 | + |
| 108 | +// Integer |
| 109 | +client.getIntegerValue(key: "my-flag", defaultValue: 1) |
| 110 | + |
| 111 | +// Double |
| 112 | +client.getDoubleValue(key: "my-flag", defaultValue: 1.1) |
| 113 | + |
| 114 | +// Object |
| 115 | +client.getObjectValue(key: "my-flag", defaultValue: Value.structure(["key":Value.integer("1234")]) |
| 116 | +``` |
| 117 | + |
| 118 | +:::note |
| 119 | +If you add a new flag in GO Feature Flag, expect some delay before having it available for the provider. |
| 120 | +Refreshing the cache from remote happens when setting a new provider and/or evaluation context in the global OpenFeatureAPI, but also when a configuration change is detected during the polling. |
| 121 | +::: |
| 122 | + |
| 123 | +### Handling Provider Events |
| 124 | +When setting the provider or the context *(via `setEvaluationContext()` or `setProvider()`)* some events can be triggered to know the state of the provider. |
| 125 | + |
| 126 | +To listen to them you can add an event handler via the `OpenFeatureAPI` shared instance: |
| 127 | + |
| 128 | +```swift |
| 129 | +OpenFeatureAPI.shared.observe().sink { event in |
| 130 | + if event == .error { |
| 131 | + // An error has been emitted |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +#### Existing type of events are: |
| 137 | +- `.ready`: Provider is ready. |
| 138 | +- `.error`: Provider in error. |
| 139 | +- `.configurationChanged`: Configuration has changed in GO Feature Flag. |
| 140 | +- `.PROVIDER_STALE`: Provider has not the latest version of the feature flags. |
| 141 | +- `.notReady`: Provider is not ready to evaluate the feature flags. |
| 142 | + |
| 143 | +## Contribute to the provider |
| 144 | +You can find the source of the provider in the [`openfeature-swift-provider`](https://github.com/go-feature-flag/openfeature-swift-provider) repository. |
0 commit comments