# Feature Flags for the Apple SDK

{{< glossary_definition "feature_flag" >}}


## Accessing flags

The Airship SDK will refresh feature flags when the app is brought to the foreground. If a feature flag is accessed before the foreground refresh completes, or after the foreground refresh has failed, feature flags will be refreshed during flag access. Feature flags will only be updated once per session and will persist for the duration of each session.

Once [defined in the dashboard](https://www.airship.com/docs/guides/experimentation/feature-flags/#create-feature-flags), a feature flag can be accessed by its name in the SDK after `takeOff`.

The SDK provides asynchronous access to feature flags using an async method, which are intended to be called from a Task or a function that supports concurrency. For more information, see [Concurrency guide](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/).

```swift
// Get the FeatureFlag
let flag: FeatureFlag = try? await Airship.featureFlagManager.flag(name: "YOUR_FLAG_NAME")

// Check if the app is eligible or not
if (flag?.isEligible == true) {
    // Do something with the flag
} else {
    // Disable feature or use default behavior
}
```


> **Note:** This feature is not supported in Objective-C.


## Tracking interaction

To generate the [Feature Flag Interaction Event](https://www.airship.com/docs/developer/rest-api/connect/schemas/events/#feature-flag-interaction), you must manually call `trackInteraction` with the feature flag. Analytics must be enabled. See: [Data Collection: Privacy Manager](https://www.airship.com/docs/developer/sdk-integration/apple/data-collection/privacy-manager/).

```swift
Airship.featureFlagManager.trackInteraction(flag: featureFlag)
```


## Error handling

If a feature flag allows evaluation with stale data, the SDK evaluates the flag if a definition for the flag is found. Otherwise, feature flag evaluation depends on updated local state. If the SDK cannot evaluate a flag because data cannot be fetched, the SDK returns or raises an error. The app can either treat the error as the flag being ineligible or retry at a later time.

```swift
do {
    let flag = try await Airship.featureFlagManager.flag(name: "YOUR_FLAG_NAME")
    if (flag.isEligible == true) {
        // Do something with the flag
    }
} catch {
    // Do something with the error
}
```


