iOS SDK Reference
This topic explains how to use the Continuous Features (CF) SDK in your iOS application.
This topic describes how to use the Harness Feature Flags iOS SDK for your iOS application.
For getting started quickly, you can use our sample code from the SDK README. You can also clone and run a sample application from the iOS SDK GitHub Repository.
The SDK caches your Feature Flags. If the cache can't be accessed, the defaultValue is used.
Before you begin
Make sure you read and understand:
Version
Latest SDK version can be found on GitHub Release Page
Requirements
Create an iOS application, or clone our sample application.
Create a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode.
Install the SDK
You can install the iOS SDK using the following dependency managers:
Swift Package Manager (SPM)
CocoaPods
Carthage
Install using SwiftPM
If you are using Swift Package Manager, you can add the SDK as a dependency in either an Xcode project or in a Package.swift file:
Xcode:
Select File > Swift Packages > Add Package Dependency and enter the iOS SDK repository clone URL, then select your desired version constraints.
Package.swift:
Add ff-ios-client-sdk to the dependencies section of your Package.swift file, for example:
Install using CocoaPods
CocoaPods is built with Ruby and can be installed with the default Ruby on macOS. You can use a Ruby Version manager, however, we recommend that you use the standard Ruby available on macOS.To install the iOS using CocoaPods, complete the following steps:
To install CocoaPods using the default Ruby available on macOS, use the
sudocommand when installing the gems, for example:
Once Cocoapods is installed, create a
Podfilein your root project folder:
Import the
ff-ios-client-sdkto your.xcproject. To do this, addff-ios-client-sdkto your newly created Podfile, for example:
Then, save the changes. 4. Use the install command to install the packages:
Make sure that you are using .xcworkspace instead of your .xcodeproj to utilize the imported Pods.
Install using Carthage
To integrate your application using Carthage, complete the following step:
Go to the root folder of your project and create a
Cartfile. This where you put all the dependencies that you use with Carthage:
Add the following line to your
Cartfile:
Fetch the source for
ff-ios-client-sdkfrom the repository specified in theCartfile:
A new folder Carthage is created at the same location as Cartfile and .xcodeproj. In the Carthage folder, another folder called Checkout is created, this folder contains the source code.
Create a project for
ff-ios-client-sdkdependency.Run the following command from your project's root folder:
Then run the following command:
Or, you can enter it all on the same line:
Build the project and place it in the
Buildfolder next toCheckouts. Run the following command from your project's root folder:
In your application targets’ General tab, in the Frameworks, Libraries, and Embedded Content, drag and drop the
.xcframeworkfile from theCarthage/Buildfolder.In Embed, select Embed & Sign.
Import the
ff_ios_client_sdk.(Optional) When a new version of
ff-ios-client-sdkis available and you want to update this dependency, run:
Import the SDK
To import the SDK, use the following command:
Initialize the SDK
To initialize the iOS SDK, you need to:
Add your Client SDK key to connect to your Harness Environment.
Add a Target that you want to Evaluate against a Feature Flag.
(Optional) Configure the SDK options.
Complete the initialization with the SDK using the Client SDK Key, Target, and Configuration parameters you set.
Add your Client SDK Key
To connect to the correct Environment that you set up on the Harness Platform, you need to add the Client SDK Key from that Environment. Input the Client SDK Key into the apiKey parameter.
Add a Target
For more information about Targets, go to Targeting Users With Flags.
To add a Target, build it using CfTarget.builder and pass in arguments for the following:
Parameter
Description
Required?
Example
identifier
ID of the Target.Read Regex requirements for Target names and identifiers below for accepted characters.
Required
.identifier("HT_1")
For example:
Configure the SDK
You can configure the following features of the SDK:
Name
Description
Default Value
baseUrl
The URL used to fetch Feature Flag Evaluations. When using the Relay Proxy, make sure the URL you provide is publicly available.
https://config.ff.harness.io/api/1.0
eventUrl
The URL for posting metrics data to the Feature Flag service. When using the Relay Proxy, make sure the URL you provide is publicly available.
https://events.ff.harness.io/api/1.0
pollInterval
The interval in seconds that we poll for changes when you are using stream mode.
60 (seconds)
streamEnabled
Set to true to enable streaming mode.Set to false to disable streaming mode.
true
analyticsEnabled
Set to true to enable analytics.Set to false to disable analytics.Note: When enabled, analytics data is posted every 60 seconds.
true
Use cfConfiguration.builder to declare the configuration options you want to use, for example:
Complete the initialization
Complete the initialization using the apiKey, configuration, and target variables, for example:
Additionally, you can also pass in a cache:onCompletion object that adopts the StorageRepositoryProtocol. This is optional, if you choose not to use it the internal built-in cache will be used instead.
Sample of initializing the SDK
initialize(apiKey:configuration:cache:onCompletion:)
Evaluate a Flag
Evaluating a Flag is when the SDK processes all Flag rules and returns the correct Variation of that Flag for the Target you provide.
If a matching Flag can’t be found, or the SDK can’t remotely fetch flags, the default value is returned.
There are different methods for the different Variation types and for each method you need to pass in:
Identifier of the Flag you want to evaluate
The default Variation
The Flag is evaluated against the Target you pass in when initializing the SDK.
Evaluate a string Variation
Evaluate a boolean Variation
Evaluate a number Variation
Evaluate a JSON Variation
ValueType can be one of the following:
ValueType.bool(Bool)
ValueType.string(String)
ValueType.int(Int)
ValueType.object([String:ValueType])
Listen for events
The registerEventsListener method provides a way to register a listener for different events that might be triggered by SDK.
The possible events, reasons for being emitted, and their responses are outlined in the following table:
EVENT_TYPE
Reason
Response
onOpen
The SDK has successfully opened a streaming connection to the Feature Flags service, and is listening for events. No further action required from your application code.
-
onComplete
The SDK has closed the streaming connection to the Feature Flags service and is no longer listening for events.
This can happen if the SDK is shutdown, of if there is a transient networking issue, and the SDK will attempt to reconnect. In both cases, no further action is required.
-
onEventListener
The SDK has streamed a new evaluation after a flag has been changed using the Harness web application. You should evaluate any flags to get the latest variation.
Note this is for individual flag changes only, and this event is not applicable to target group changes.
See EVALUATION_RELOAD for streaming target group changes.
Evaluation
onPolling
This event can be emitted for 3 reasons.
A target group change has been made using the Harness web application, and all evaluations has been reloaded.
Streaming has been disabled and the SDK is in polling mode, and a poll has completed which has reloaded all evaluations.
The SDK has disconnected from the stream and has entered polling mode, and a poll has completed which has reloaded all evaluations. |
[Evaluation]|
Test your app is connected to Harness
When you receive a response showing the current status of your Feature Flag, go to the Harness Platform and toggle the Flag on and off. Then, check your app to verify if the Flag Variation displayed is updated with the Variation you toggled.
Close the SDK client
In most applications, you won't need to close the SDK client.
However, you should close the SDK client if:
Your application is about to terminate. Closing the client ensures that all associated resources are released.
You have determined that you do not need to evaluate flags again in your application lifecycle.
To close the SDK client, call this method:
You also need to call this method when changing accounts on the CfTarget object to re-initialize and fetch Evaluations for the correct Target.
Additional options
Using our public API methods
You can also use the public API methods to initialize and implement the Feature Flag iOS SDKs. All the below methods are called on CfClient.sharedInstance.
The Public API exposes the following methods that you can utilize:
Sample code for an iOS application
Here is a sample code for using the Harness iOS SDK with an iOS application:
Troubleshooting
The SDK logs the following codes for certain lifecycle events, for example authentication, which can aid troubleshooting.
Code
Description
Log Level
1000
Successfully initialized
Info
1001
Failed to initialize due to authentication error
Error
1002
Failed to initialize due to a missing or empty API key
Error
1003
WaitForInitialization configuration option was provided and the SDK is waiting for initialization to finish
Info
2000
Successfully authenticated
Info
2001
Authentication failed with a non-recoverable error
Error
2002
Authentication failed and is retrying
Warn
2003
Authentication failed and max retries have been exceeded
Error
3000
SDK closing
Info
3001
SDK closed successfully
Info
4000
Polling service started
Info
4001
Polling service stopped
Info
5000
Streaming connected
Info
5001
Streaming disconnected
Warn
5002
Streaming event received
Debug
5003
Streaming disconnected and is retrying to connect
Info
5004
Streaming service stopped
Info
5005
Stream is still retrying to connect after 4 attempts
Warn
6000
Evaluation was successful
Debug
6001
Evaluation failed and the default value was returned
Info
7000
Metrics service has started
Info
7001
Metrics service has stopped
Info
7002
Metrics posting failed
Warn
7003
Metrics posting success
Debug
Last updated
Was this helpful?