> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/feature-management-experimentation/new-to-fme/sdks-and-customer-deployed-components/openfeature-providers/ios-sdk.md).

# OpenFeature Provider for iOS SDK

Integrate your iOS applications with Harness FME using the iOS OpenFeature Provider, a standardized, vendor-agnostic feature flagging API. This provider implements the OpenFeature specification and bridges the OpenFeature SDK with the Harness FME iOS SDK.

This page walks you through installing, configuring, and using the iOS OpenFeature provider to evaluate feature flags in your iOS applications.

### Before you begin <a href="#before-you-begin" id="before-you-begin"></a>

Before you begin, ensure you have the following:

* A valid [Harness FME SDK key](/feature-management-experimentation/new-to-fme/sdks-and-customer-deployed-components.md#api-keys) for your project
* An iOS project (Xcode 14+)
* Access to your project's `Package.swift` file or Xcode project to add the provider dependency

### Version compatibility <a href="#version-compatibility" id="version-compatibility"></a>

| Component                                         | Minimum Version |
| ------------------------------------------------- | --------------- |
| iOS                                               | ≥ 14            |
| `@splitsoftware/split-openfeature-provider-swift` | ≥ 1.0.0         |
| OpenFeature Swift SDK                             | ≥ 0.4.0         |

### Install the provider and dependencies <a href="#install-the-provider-and-dependencies" id="install-the-provider-and-dependencies"></a>

Add the Harness FME OpenFeature provider dependency to your application's Xcode project using Swift Package Manager.

```swift
.package(url: "https://github.com/splitio/split-openfeature-provider-swift", from: "1.0.0")
```

### Initialize the provider <a href="#initialize-the-provider" id="initialize-the-provider"></a>

The Harness FME OpenFeature provider requires an iOS application `Context` (for SDK initialization) and your Harness FME SDK Key.

```swift
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 with initial context
    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)
}
```

### Construct an evaluation context <a href="#construct-an-evaluation-context" id="construct-an-evaluation-context"></a>

Provide an evaluation context with a targeting key to evaluate flags. The evaluation context passes targeting information such as user IDs, email addresses, or plan types for flag targeting.

For example:

```swift
let context = ImmutableContext(
    targetingKey: "martin", 
    structure: ImmutableStructure(attributes: [ 
        "email": Value.String(someValue),
        "age": Value.Integer(30)
]))
OpenFeatureAPI.shared.setEvaluationContext(evaluationContext: context)

let client = OpenFeatureAPI.getClient()
let result = client.getBooleanDetails("premium-feature", false, context)
```

To set a targeting key during initialization:

```swift
let initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)
```

To change a targeting key at runtime:

```swift
let newContext = ImmutableContext(targetingKey = "user-456")
OpenFeatureAPI.setEvaluationContext(newContext)
```

### Observe provider events <a href="#observe-provider-events" id="observe-provider-events"></a>

The Harness FME OpenFeature provider emits events when provider state changes (for example, when flags update, configuration changes, or errors) occur. You can observe these events to update your application's behavior in real time.

You can enable your application to:

* Refresh feature-dependent UI when flags change
* Gracefully handle degraded states (i.e., when the provider becomes stale)
* Log or alert on configuration or network issues
* Control initialization flows based on provider readiness

For example:

```swift
import Combine

let cancellable = OpenFeatureAPI.shared.observe().sink { event in
    switch event {
    case ProviderEvent.ready:
        // Provider ready
    case ProviderEvent.stale:
        // Provider stale
    case ProviderEvent.configurationChanges:
        // Configuration changed
    case ProviderEvent.contextChanged:
        // Context updated
    case ProviderEvent.error(let errorCode, let message):
        // Handle error
    default:
        break
    }
}
```

For more information, go to the [Harness FME iOS OpenFeature Provider GitHub repository](https://github.com/splitio/split-openfeature-provider-swift/).
