iOS SDK
This client-side Swift SDK helps you integrate with Unleash and evaluate feature flags inside your application.
You can use this client with Unleash Enterprise or Unleash Open Source.
You can connect the SDK to Unleash in two ways:
- Directly to your Unleash instance using the Frontend API.
- Using Unleash Edge, a lightweight service acting as a caching and evaluation layer between the SDK and your main Unleash instance. Unleash Edge fetches flag configurations, caches them in-memory, and handles evaluation locally for faster responses and high availability.
In both setups, the SDK retrieves feature flag configurations for the provided context. The SDK caches the received flag configurations in memory and refreshes them periodically (at a configurable interval). This makes local evaluations like isEnabled() extremely fast.
Requirements
- MacOS: 12.15
- iOS: 12
Usage
To get started, import the SDK and initialize the Unleash client:
iOS >= 13
iOS >= 12
In the example above we import the UnleashProxyClientSwift and instantiate the client. You need to provide the following parameters:
unleashUrl: The full URL to either the Unleash Frontend API or an Unleash Edge instance [String]clientKey: A frontend API token for authenticating with the Frontend API or Unleash Edge [String]refreshInterval: The polling interval in seconds, set to0to only poll once and disable a periodic polling [Int]appName: The application name identifier [String]context: Initial Unleash context fields (likeuserId,sessionId, etc.), excludingappNameandenvironmentwhich are configured separately. [String: String]
Calling unleash.start() makes the initial request to retrieve the feature flag configuration and starts the background polling interval (if refreshInterval > 0).
ready event), checking a feature flag might return the default value (often false). To ensure the configuration is loaded before checking flags, subscribe to the ready event. See the Events section for details.Once the configuration is loaded, you can check if a feature flag is enabled:
You can also set up variants:
Available Options
The Unleash SDK accepts the following initialization options:
Bootstrapping
You can provide the initial feature flag state to the Unleash client SDK. This is useful when you have a known initial state for your feature flag. This state can be bootstrapped to the client via a list of Toggle objects, or from a file matching a response from the Frontend API. For example:
Bootstrap From Hard-coded List
Bootstrap From File Matching Frontend API Response
Using the Bootstrap List of Toggles
Whether from a hard-coded list, or a json file, the bootstrap toggles can be injected into the Unleash Edge or Proxy client either at initialisation time, or when calling start. For example:
Important Notices
- If you initialise the Unleash Edge client with a
Poller, inject the bootstrap directly into the poller. Any bootstrap data injected into the Unleash client options will be ignored when a custom poller is also provided. - Bootstrapped flag configurations are replaced entirely after the first successful fetch.
- If bootstrap flags are provided when calling start, the first fetch occurs after the configured
refreshInterval(default 15 seconds). - Calling
updateContext(...)before the first fetch removes any bootstrapped flags.
Update Context
To update the context, use the following method:
This will stop and start the polling interval in order to renew polling with new context values.
You can use any of the predefined fields. If you need to support custom properties pass them as the second argument:
Custom PollerSession
If you want to use a custom URLSession or intercept URLRequest you can provide a custom PollerSession to the client.
Custom HTTP Headers
If you want the client to send custom HTTP headers with all requests to the Unleash API you can define that by setting them via the UnleashClientBase.
Custom and dynamic custom headers does not apply to sensitive headers.
Content-TypeIf-None-Match- anything starting with
unleash-(unleash-appname,unleash-connection-id,unleash-sdk, …)
Dynamic Custom HTTP Headers
If you need custom HTTP headers that change during the lifetime of the client, a provider can be defined via the UnleashClientBase.
Events
The client emits events that you can subscribe to using the subscribe(name:callback:) method or the UnleashEvent enum.
Standard Events
ready(UnleashEvent.ready): Emitted once the client has successfully fetched and cached the initial feature flag configurations.update(UnleashEvent.update): Emitted when a subsequent fetch results in a change to the feature flag configurations.sent(UnleashEvent.sent): Emitted when usage metrics have been successfully sent to the server.error(UnleashEvent.error): Emitted if an error occurs when trying to send metrics.
Subscribing to Standard Events
Alternatively you can use the enum UnleashEvent, for example:
Impression Data Events
This SDK allows you to subscribe to Impression Data events. These events provide granular, real-time tracking of feature exposures. You must specifically enable impression data for the feature flags you’d like to track.
When isEnabled(name:) or getVariant(name:) is called for a feature flag that has impression data enabled, the SDK creates an ImpressionEvent object that is broadcast internally using the event name impression (also accessible via the UnleashEvent.impression enum case).
Subscribing to Impression Data Events
Releasing
Note: To release the package you’ll need to have CocoaPods installed.
Update Sources/Version/Version.swift with the new version number. This version is used in unleash-sdk header as a version reported to Unleash server.
Then, add a Git tag matching the version number. Releasing the tag is sufficient for the Swift package manager, but you might also want to ensure CocoaPods users can also consume the code.
Please make sure that the tag is pushed to the remote.
The next few commands assume that you have CocoaPods installed and available on your shell.
First, validate your session with CocoaPods with the following command:
The email that owns this package is the general Unleash team email. CocoaPods will send a link to this email, click it to validate your shell session.
Bump the version number of the package, you can find this in UnleashProxyClientSwift.podspec, we use SemVer for this project. Once that’s committed and merged to main:
Linting the podspec is always a good idea:
Once that succeeds, you can do the actual release:
Testing
In order to test this package you can run the swift test command. To test thread safety, run swift test with:
This gives you warnings in the console when you have any data races.
Installation
Follow the following steps in order to install the unleash-ios-sdk:
- In your Xcode project go to File -> Swift Packages -> Add Package Dependency
- Supply the link to this repository
- Set the appropriate package constraints (typically up to next major version)
- Let Xcode find and install the necessary packages
Once you’re done, you should see SwiftEventBus and UnleashProxyClientSwift listed as dependencies in the file explorer of your project.
Upgrade Guide From 1.x -> 2.x
In 2.0.0 the StorageProvider public interface was changed to be more in line with other SDKs. Specifically the set method was changed to accept all flags at once. It now has the following interface:
If you are running with your own StorageProvider implementation you’ll need to make changes to your implementation.