Skip to content

Commit 3313706

Browse files
Merge branch 'main' into dependabot/go_modules/golang.org/x/net-0.27.0
2 parents de4b7e1 + a91cd4d commit 3313706

File tree

4 files changed

+161
-1
lines changed

4 files changed

+161
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,10 @@ For now, we have providers for:
284284
| Javascript/Typescript (client) | [Client provider](https://github.com/open-feature/js-sdk-contrib/tree/main/libs/providers/go-feature-flag-web) | ![version](https://img.shields.io/npm/v/%40openfeature%2Fgo-feature-flag-web-provider?color=blue&style=flat-square) |
285285
| Python | [Python provider](openfeature/providers/python-provider) | ![version](https://img.shields.io/pypi/v/gofeatureflag-python-provider?color=blue&style=flat-square) |
286286
| .Net | [.Net Provider](https://github.com/open-feature/dotnet-sdk-contrib/tree/main/src/OpenFeature.Contrib.Providers.GOFeatureFlag) | ![version](https://img.shields.io/nuget/v/OpenFeature.Contrib.GOFeatureFlag?color=blue&style=flat-square) |
287+
| Swift | [Swift Provider](https://github.com/go-feature-flag/openfeature-swift-provider) | ![version](https://img.shields.io/github/v/release/go-feature-flag/openfeature-swift-provider?label=Swift&display_name=tag&style=flat-square&logo=Swift) |
287288
| PHP | Not currently available [help by contributing here](https://github.com/open-feature/php-sdk-contrib/) | |
288289
| Ruby | Not currently available [help by contributing here](https://github.com/open-feature/ruby-sdk-contrib) | |
289-
| Swift | Not currently available [help by contributing here](https://github.com/open-feature/swift-sdk) | |
290+
290291

291292
## Where do I store my flags file?
292293

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
[![version](https://img.shields.io/github/v/release/go-feature-flag/openfeature-swift-provider?label=Swift&display_name=tag&style=flat-square&logo=Swift)](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.

website/docs/openfeature_sdk/sdk.mdx

+9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ Rest assured, working with OpenFeature SDKs alongside GO Feature Flag providers
8585
badgeUrl={"https://img.shields.io/npm/v/%40openfeature%2Fgo-feature-flag-web-provider?color=blue&style=flat-square"}
8686
features={["remoteEval", "localCache", "dynamicRefresh"]}/>
8787
},
88+
{
89+
logoCss: "devicon-swift-plain colored",
90+
title:"Swift (iOS/tvOS/macOS)",
91+
badges:["Client"],
92+
docLink: "./client_providers/openfeature_swift",
93+
content: <SdkCardContent
94+
badgeUrl={"https://img.shields.io/maven-central/v/org.gofeatureflag.openfeature/gofeatureflag-kotlin-provider?color=blue&style=flat-square"}
95+
features={["remoteEval", "localCache", "dynamicRefresh"]}/>
96+
},
8897
{
8998
logoCss: "devicon-android-plain colored",
9099
title:"Android / Kotlin",

website/src/components/home/features/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ function OpenFeature() {
147147
fontAwesomeIcon="devicon-react-original colored"
148148
tooltipText="React"
149149
/>
150+
151+
<SocialIcon
152+
colorClassName={styles.socialIconCyan}
153+
fontAwesomeIcon="devicon-swift-plain colored"
154+
tooltipText="Swift (iOS/tvOS/macOS)"
155+
/>
150156
</div>
151157
</div>
152158
<div className="col-1-2">

0 commit comments

Comments
 (0)