Skip to main content

iOS SDK Getting Started

CocoaPods compatible Carthage compatible SwiftPM compatible GitHub

Initializing the SDK

We recommend initializing the SDK once in didFinishLaunchingWithOptions of your AppDelegate for iOS / tvOS, or applicationDidFinishLaunching for macOS, to receive features for as soon as possible and to pass around the client instance around in your app.

Swift

Using the builder pattern we can initialize the DevCycle SDK by providing the DevCycleUser and DevCycle mobile SDK key:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

...

let user = try DevCycleUser.builder()
.userId("my-user1")
.build()

guard let devcycleClient = try DevCycleClient.builder()
.sdkKey("<DEVCYCLE_MOBILE_SDK_KEY>")
.user(user)
.build(onInitialized: nil)
self.devcycleClient = devcycleClient

...

return true
}

The user object needs either a user_id, or isAnonymous set to true for an anonymous user.

Objective-C

For Objective-C we use a standard callback pattern to initialize the DevCycle SDK by providing the DevCycleUser and DevCycle mobile SDK key:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

...

DevCycleUser *user = [DevCycleUser initializeWithUserId:@"my-user1"];

self.devcycleClient = [DevCycleClient initialize:@"<DEVCYCLE_MOBILE_SDK_KEY>"
user:user
options:nil
onInitialized:^(NSError * _Nullable error) {
if (error) {
NSLog(@"DevCycle failed to initialize: %@", error);
}
}];

...

return YES;
}

DevCycleClientBuilder

The DevCycleClient can be built using the following methods:

DevCycleClientBuilder class

MethodParameterDescription
sdkKeyStringDevCycle SDK key
userDevCycleUserDevCycle user object
optionsDevCycleOptionsDevCycle options object

DevCycleUser Builder

The DevCycleUser can be built using the following methods:

DevCycleUserBuilder class

MethodParameterDescription
userIdStringUnique user ID
isAnonymousBoolBoolean to indicate if the user is anonymous
emailStringUser's email
nameStringUser's name
languageStringUser's language
countryStringUser's country
customData[String: Any]Key/value map of properties to be used for targeting
privateCustomData[String: Any]Key/value map of properties to be used for targeting. Private properties will not be included in event logging.

DevCycleOptions Builder

The SDK exposes various initialization options which can be used by passing a DevCycleOptions object to the withOptions method of DevCycleClient.builder():

DevCycleOptionsBuilder class

MethodParameterDefaultDescription
flushEventsIntervalMsInt10000Controls the interval between flushing events to the DevCycle servers in milliseconds, defaults to 10 seconds.
disableCustomEventLoggingBooleanfalseDisables logging of custom events generated by calling .track() method to DevCycle.
disableAutomaticEventLoggingBooleanfalseDisables logging of SDK generated events (e.g. variableEvaluated, variableDefaulted) to DevCycle.
logLevelLogLevelerrorSet log level of the default logger. Defaults to error
enableEdgeDBBooleanfalseEnables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle.
configCacheTTLInt604800000The maximum allowed age of a cached config in milliseconds, defaults to 7 days
disableConfigCacheBoolfalseDisable the use of cached configs
disableRealtimeUpdatesBoolfalseDisable Realtime Updates
apiProxyURLStringnilAllows the SDK to communicate with a proxy of DevCycle Bucketing API / Client SDK API.

Notifying when DevCycle features are available

In the initialize call there is an optional onInitialized parameter you can use to determine when your features have been loaded:

Swift

self.devcycleClient = try? DevCycleClient.builder()
.sdkKey("<DEVCYCLE_MOBILE_SDK_KEY>")
.user(user)
.options(options)
.build(onInitialized: { error in
if (error != nil) {
// there was an error with building the client
} else {
// initialized successfully
}
})

Objective-C

self.devcycleClient = [DevCycleClient initialize:@"<DEVCYCLE_MOBILE_SDK_KEY>"
user:user
options:nil
onInitialized:^(NSError * _Nullable error) {
if (error) {
NSLog(@"DevCycle failed to initialize: %@", error);
} else {
// initialized successfully
}
}];