iOS SDK Getting Started
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 set, or isAnonymous = true for an anonymous user.
An empty DevCycleUser will default to an anonymous user, but it is recommended to set the user_id or isAnonymous = true.
// example anonymous user
let user = try DevCycleUser.builder()
.isAnonymous(true)
.build()
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:
| Method | Parameter | Description |
|---|---|---|
| sdkKey | String | DevCycle SDK key |
| user | DevCycleUser | DevCycle user object |
| options | DevCycleOptions | DevCycle options object |
DevCycleUser Builder
The DevCycleUser can be built using the following methods:
| Method | Parameter | Description | Auto-Populated |
|---|---|---|---|
| userId | String | Unique user ID | No |
| isAnonymous | Bool | Boolean to indicate if the user is anonymous | No |
| String | User's email | No | |
| name | String | User's name | No |
| language | String | User's language | No |
| country | String | User's country | No |
| appVersion | String | App version | Yes |
| customData | [String: Any] | Key/value map of properties to be used for targeting | No |
| privateCustomData | [String: Any] | Key/value map of properties to be used for targeting. Private properties will not be included in event logging. | No |
| platform | String | Platform/OS | Yes |
| platformVersion | String | Platform/OS Version | Yes |
| deviceModel | String | Device Machine Code (ex: iPhone1,1, iPad15,8) | Yes |
DevCycleOptions Builder
The SDK exposes various initialization options which can be used by passing a DevCycleOptions object to the withOptions method of DevCycleClient.builder():
| Method | Parameter | Default | Description |
|---|---|---|---|
| flushEventsIntervalMs | Int | 10000 | Controls the interval between flushing events to the DevCycle servers in milliseconds, defaults to 10 seconds. |
| disableCustomEventLogging | Boolean | false | Disables logging of custom events generated by calling .track() method to DevCycle. |
| disableAutomaticEventLogging | Boolean | false | Disables logging of SDK generated events (e.g. variableEvaluated, variableDefaulted) to DevCycle. |
| logLevel | LogLevel | error | Set log level of the default logger. Defaults to error |
| enableEdgeDB | Boolean | false | Enables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle. |
| configCacheTTL | Int | 2592000000 | The maximum allowed age of a cached config in milliseconds, defaults to 30 days |
| disableConfigCache | Bool | false | Disable the use of cached configs |
| disableRealtimeUpdates | Bool | false | Disable Realtime Updates |
| apiProxyURL | String | nil | Allows the SDK to communicate with a proxy of DevCycle Client SDK API. |
| eventsApiProxyURL | String | nil | Allows the SDK to communicate with a proxy of DevCycle Events API. |
Notifying when DevCycle features are available
In the initialize call there is an optional onInitialized callback 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
}
})
you can also await the .build() method:
self.devcycleClient = try? await DevCycleClient.builder()
.sdkKey("<DEVCYCLE_MOBILE_SDK_KEY>")
.user(user)
.options(options)
.build()
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
}
}];