@@ -35,36 +35,64 @@ github "DevCycleHQ/ios-client-sdk"
3535
3636### Initializing the SDK
3737
38+ #### Swift
3839Using the builder pattern we can initialize the DevCycle SDK by providing the DVCUser and DevCycle mobile environment key:
3940
4041``` swift
4142let user = try DVCUser.builder ()
4243 .userId (" my-user1" )
4344 .build ()
4445
45- guard let client = try DVCClient.builder ()
46+ guard let dvcClient = try DVCClient.builder ()
4647 .environmentKey (" <DEVCYCLE_MOBILE_ENVIRONMENT_KEY>" )
4748 .user (user)
4849 .build (onInitialized : nil )
4950```
5051
5152The user object needs either a `user_id`, or `isAnonymous` set to `true ` for an anonymous user.
5253
54+ #### Objective- C
55+ For Objective- C we use a standard callback pattern to initialize the DevCycle SDK by providing the DVCUser and DevCycle mobile environment key:
56+
57+ ```objc
58+ DVCUser * user = [DVCUser initializeWithUserId: @" my-user1" ];
59+
60+
61+ self .dvcClient = [DVCClient initialize: @" <DEVCYCLE_MOBILE_ENVIRONMENT_KEY>"
62+ user: user
63+ options: nil
64+ onInitialized: ^ (NSError * _Nullable error) {
65+ if (error) {
66+ NSLog (@" DevCycle failed to initialize: %@" , error);
67+ }
68+ }];
69+ ```
70+
5371## Using Variable Values
5472
5573To get values from your Features, the ` variable() ` method is used to fetch variable values using
5674the variable's identifier ` key ` coupled with a default value. The default value can be of type
5775string, boolean, number, or JSONObject:
5876
77+ #### Swift
5978``` swift
60- let strVariable: DVCVariable< String > = client.variable (key : " str_key" , defaultValue : " default" )
61- let boolVariable: DVCVariable< Bool > = client.variable (key : " bool_key" , defaultValue : false )
62- let numVariable: DVCVariable< Int > = client.variable (key : " num_key" , defaultValue : 4 )
63- let jsonVariable: DVCVariable< [String : Any ]> = client.variable (key : " json_key" , defaultValue : [: ])
79+ let boolVariable = dvcClient.variable (key : " bool_key" , defaultValue : false )
80+ let strVariable = dvcClient.variable (key : " string_key" , defaultValue : " default" )
81+ let numVariable = dvcClient.variable (key : " num_key" , defaultValue : 4 )
82+ let jsonVariable = dvcClient.variable (key : " json_key" , defaultValue : [: ])
83+ ```
84+
85+ #### Objective-C
86+ ``` objc
87+ DVCVariable *boolVariable = [self .dvcClient boolVariableWithKey: @"bool_key" defaultValue: false ] ;
88+ DVCVariable * strVariable = [ self.dvcClient stringVariableWithKey:@"string_key" defaultValue:@"default"] ;
89+ DVCVariable * numVariable = [ self.dvcClient numberVariableWithKey:@"num_key" defaultValue:@4 ] ;
90+ DVCVariable * jsonVariable = [ self.dvcClient jsonVariableWithKey:@"json_key" defaultValue:@{}] ;
6491```
6592
6693To grab the value, there is a property on the object returned to grab the value:
6794
95+ #### Swift
6896```swift
6997if (boolVariable.value == true) {
7098 // Run Feature Flag Code
@@ -73,6 +101,33 @@ if (boolVariable.value == true) {
73101}
74102```
75103
104+ #### Objective-C
105+ ``` objc
106+ if (boolVariable.value == true ) {
107+ // Run Feature Flag Code
108+ } else {
109+ // Run Default Code
110+ }
111+ ```
112+
113+ To listen for updates on the variable's ` value ` , for example when the user's ` userId ` is changed or ` resetUser() ` is called, an ` onUpdate() ` handler can be used:
114+
115+ #### Swift
116+ ``` swift
117+ let boolVariable = dvcClient.variable (key : " bool_key" , defaultValue : false )
118+ .onUpdate { value in
119+ // Variable value updated
120+ }
121+ ```
122+
123+ #### Objective-C
124+ ``` objc
125+ DVCVariable *boolVar = [[self .dvcClient boolVariableWithKey: @"bool_key" defaultValue: true ]
126+ onUpdateWithHandler:^(id _ Nonnull value) {
127+ // Variable value updated
128+ }] ;
129+ ```
130+
76131The `Variable` object also contains the following params:
77132 - `key`: the key indentifier for the Variable
78133 - `type`: the type of the Variable, one of: `String` / `Boolean` / `Number` / `JSON`
@@ -87,9 +142,16 @@ If the value is not ready, it will return the default value passed in the creati
87142
88143To grab all the Features or Variables returned in the config:
89144
145+ #### Swift
90146```swift
91- let features: [String : Feature] = client.allFeatures ()
92- let variables: [String : Variable] = client.allVariables ()
147+ let features: [String: Feature] = dvcClient.allFeatures()
148+ let variables: [String: Variable] = dvcClient.allVariables()
149+ ```
150+
151+ #### Objective-C
152+ ``` objc
153+ NSDictionary *allFeatures = [self .dvcClient allFeatures ];
154+ NSDictionary *allVariables = [self .dvcClient allVariables ];
93155```
94156
95157If the SDK has not finished initializing, these methods will return an empty object.
@@ -99,29 +161,48 @@ If the SDK has not finished initializing, these methods will return an empty obj
99161To identify a different user, or the same user passed into the initialize method with more attributes,
100162build a DVCUser object and pass it into ` identifyUser ` :
101163
164+ #### Swift
102165``` swift
103- let user = try DVCUser.builder ()
104- .userId (" my-user1" )
105- 106- .appBuild (1005 )
107- .appVersion (" 1.1.1" )
108- .country (" CA" )
109- .name (" My Name" )
110- .language (" EN" )
111- .customData ([
112- " customkey" : " customValue"
113- ])
114- .privateCustomData ([
115- " customkey2" : " customValue2"
116- ])
117- .build ()
118- client.identifyUser (user)
166+ do {
167+ let user = try DVCUser.builder ()
168+ .userId (" my-user1" )
169+ 170+ .country (" CA" )
171+ .name (" My Name" )
172+ .language (" EN" )
173+ .customData ([ " customkey" : " customValue" ])
174+ .privateCustomData ([ " customkey2" : " customValue2" ])
175+ .build ()
176+ try dvcClient.identifyUser (user : user)
177+ } catch {
178+ print (" Error building new DVCUser: \( error ) " )
179+ }
180+ ```
181+
182+ #### Objective-C
183+ ``` objc
184+ DVCUser *user = [DVCUser initializeWithUserId: @"my-user1"] ;
185+ user.email = @"
[email protected] ";
186+ user.appBuild = @1005 ;
187+ user.appVersion = @"1.1.1";
188+ user.country = @"CA";
189+ user.name = @"My Name";
190+ user.language = @"EN";
191+ user.customData = @{@"customKey": @"customValue"};
192+ user.privateCustomData = @{@"customkey2": @"customValue2"};
193+
194+ [ self.dvcClient identifyUser: user callback:^(NSError * error, NSDictionary<NSString * ,id> * variables) {
195+ if (error) {
196+ return NSLog(@"Error calling DVCClient identifyUser:callback: %@", error);
197+ }
198+ }] ;
119199```
120200
121201To wait on Variables that will be returned from the identify call, you can pass in a DVCCallback:
122202
203+ #### Swift
123204```swift
124- try client .identifyUser (user) { error, variables in
205+ try dvcClient .identifyUser(user: user) { error, variables in
125206 if (error != nil) {
126207 // error identifying user
127208 } else {
@@ -130,31 +211,62 @@ try client.identifyUser(user) { error, variables in
130211}
131212```
132213
214+ #### Objective-C
215+ ``` objc
216+ [self .dvcClient identifyUser: user callback:^(NSError * error, NSDictionary<NSString * ,id> * variables) {
217+ if (error) {
218+ // error identifying user
219+ } else {
220+ // use variables
221+ }
222+ }] ;
223+ ```
224+
133225If `error` exists the called the user's configuration will not be updated and previous user's data will persist.
134226
135227## Reset User
136228
137229To reset the user into an anonymous user, `resetUser` will reset to the anonymous user created before
138230or will create one with an anonymous `user_id`.
139231
232+ #### Swift
140233```swift
141- client.resetUser ()
234+ try dvcClient.resetUser()
235+ ```
236+
237+ #### Objective-C
238+ ``` objc
239+ [self .dvcClient resetUser: nil] ;
142240```
143241
144242To wait on the Features of the anonymous user, you can pass in a DVCCallback:
145243
244+ #### Swift
146245```swift
147- try client .resetUser { error, variables in
246+ try dvcClient .resetUser { error, variables in
148247 // anonymous user
149248}
150249```
151250
251+ #### Objective-C
252+ ``` objc
253+ [self .dvcClient resetUser: ^(NSError * error, NSDictionary<NSString * ,id> * variables) {
254+ if (error) {
255+ // Error resetting user, existing user used
256+ } else {
257+ // anonymous user
258+ }
259+ }] ;
260+ ```
261+
262+
152263If `error` exists is called the user's configuration will not be updated and previous user's data will persist.
153264
154265## Tracking Events
155266
156267To track events, pass in an object with at least a `type` key:
157268
269+ #### Swift
158270```swift
159271let event = try DVCEvent.builder()
160272 .type("my_event")
@@ -163,11 +275,27 @@ let event = try DVCEvent.builder()
163275 .metaData([ "key": "value" ])
164276 .clientDate(Date())
165277 .build()
166- client.track (event)
278+ dvcClient.track(event)
279+ ```
280+
281+ #### Objective-C
282+ ``` objc
283+ NSError *err = nil ;
284+ DVCEvent *event = [DVCEvent initializeWithType: @"my-event"] ;
285+ [ self.dvcClient track: event err:&err] ;
286+ if (err) {
287+ NSLog(@"Error calling DVCClient track:err: %@", err);
288+ }
167289```
168290
169291The SDK will flush events every 10s or `flushEventsMS` specified in the options. To manually flush events, call:
170292
293+ #### Swift
171294```swift
172- client.flushEvents ()
295+ dvcClient.flushEvents()
296+ ```
297+
298+ #### Objective-C
299+ ``` objc
300+ [self .dvcClient flushEvents ];
173301```
0 commit comments