Skip to content

Commit cc9106d

Browse files
committed
add sample codes for Swift and ObjC
1 parent 6406f25 commit cc9106d

File tree

7 files changed

+235
-96
lines changed

7 files changed

+235
-96
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#import "VariationViewController.h"
1919
#import "FailureViewController.h"
2020
#import "CustomLogger.h"
21+
#import "SamplesForAPI.h"
2122

2223
@import Optimizely;
2324
#if TARGET_OS_IOS

DemoObjCApp/DemoObjcApp.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
6E9681D921EFA5B2009B7FEC /* CustomLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E9681D721EFA5B2009B7FEC /* CustomLogger.m */; };
3535
6EB8456B21EE5DE3005346CC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EB8456421EE5DE3005346CC /* AppDelegate.m */; };
3636
6EB8456C21EE5DE3005346CC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EB8456421EE5DE3005346CC /* AppDelegate.m */; };
37+
6EFA3206227CD55E006FA572 /* SamplesForAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA3205227CD55E006FA572 /* SamplesForAPI.m */; };
38+
6EFA3207227CD55E006FA572 /* SamplesForAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA3205227CD55E006FA572 /* SamplesForAPI.m */; };
3739
A0C60F82A88EDB74234D7D3F /* Pods_DemoObjctvOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 48C9592909E0CF5074F9A84D /* Pods_DemoObjctvOS.framework */; };
3840
/* End PBXBuildFile section */
3941

@@ -189,6 +191,8 @@
189191
6EF7498E21E404BB008B22A0 /* DemoObjciOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoObjciOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
190192
6EF7499F21E404BC008B22A0 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
191193
6EF749A821E404D6008B22A0 /* DemoObjctvOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoObjctvOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
194+
6EFA3204227CD55E006FA572 /* SamplesForAPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SamplesForAPI.h; sourceTree = "<group>"; };
195+
6EFA3205227CD55E006FA572 /* SamplesForAPI.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SamplesForAPI.m; sourceTree = "<group>"; };
192196
762D1F9E4AA702083D17C4C9 /* Pods-DemoObjciOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoObjciOS.release.xcconfig"; path = "../Pods/Target Support Files/Pods-DemoObjciOS/Pods-DemoObjciOS.release.xcconfig"; sourceTree = "<group>"; };
193197
D47F5FC88600AFD4E656D50A /* Pods-DemoObjciOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoObjciOS.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-DemoObjciOS/Pods-DemoObjciOS.debug.xcconfig"; sourceTree = "<group>"; };
194198
D58542E1B5771327083E56E2 /* Pods-DemoObjctvOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoObjctvOS.release.xcconfig"; path = "../Pods/Target Support Files/Pods-DemoObjctvOS/Pods-DemoObjctvOS.release.xcconfig"; sourceTree = "<group>"; };
@@ -312,6 +316,7 @@
312316
6EB8456321EE5DE3005346CC /* SplashScreenViewController.h */,
313317
6EB8456921EE5DE3005346CC /* SplashScreenViewController.m */,
314318
6E96812021EE957B009B7FEC /* Customization */,
319+
6EFA322A227CDDF0006FA572 /* Samples */,
315320
6E96817721EEA321009B7FEC /* DemoObjciOS */,
316321
6E96817321EEA321009B7FEC /* DemoObjctvOS */,
317322
6E9681A121EEA3D8009B7FEC /* SharedResources */,
@@ -331,6 +336,15 @@
331336
name = Products;
332337
sourceTree = "<group>";
333338
};
339+
6EFA322A227CDDF0006FA572 /* Samples */ = {
340+
isa = PBXGroup;
341+
children = (
342+
6EFA3204227CD55E006FA572 /* SamplesForAPI.h */,
343+
6EFA3205227CD55E006FA572 /* SamplesForAPI.m */,
344+
);
345+
path = Samples;
346+
sourceTree = "<group>";
347+
};
334348
E4F9C3F25D5A22985B6E41CD /* Frameworks */ = {
335349
isa = PBXGroup;
336350
children = (
@@ -640,6 +654,7 @@
640654
6E9681BA21EEB361009B7FEC /* VariationViewController.m in Sources */,
641655
6EB8456B21EE5DE3005346CC /* AppDelegate.m in Sources */,
642656
6E9681D821EFA5B2009B7FEC /* CustomLogger.m in Sources */,
657+
6EFA3206227CD55E006FA572 /* SamplesForAPI.m in Sources */,
643658
6E9681BB21EEB361009B7FEC /* FailureViewController.m in Sources */,
644659
6E4DD7F721E51E5500B0C2C7 /* main.m in Sources */,
645660
6E9681BC21EEB361009B7FEC /* SplashScreenViewController.m in Sources */,
@@ -653,6 +668,7 @@
653668
6E9681BD21EEB364009B7FEC /* VariationViewController.m in Sources */,
654669
6EB8456C21EE5DE3005346CC /* AppDelegate.m in Sources */,
655670
6E9681D921EFA5B2009B7FEC /* CustomLogger.m in Sources */,
671+
6EFA3207227CD55E006FA572 /* SamplesForAPI.m in Sources */,
656672
6E9681BE21EEB364009B7FEC /* FailureViewController.m in Sources */,
657673
6E4DD7F821E51E5600B0C2C7 /* main.m in Sources */,
658674
6E9681BF21EEB364009B7FEC /* SplashScreenViewController.m in Sources */,

DemoObjCApp/Samples/SamplesForAPI.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
/****************************************************************************
3+
* Copyright 2019, Optimizely, Inc. and contributors *
4+
* *
5+
* Licensed under the Apache License, Version 2.0 (the "License"); *
6+
* you may not use this file except in compliance with the License. *
7+
* You may obtain a copy of the License at *
8+
* *
9+
* http://www.apache.org/licenses/LICENSE-2.0 *
10+
* *
11+
* Unless required by applicable law or agreed to in writing, software *
12+
* distributed under the License is distributed on an "AS IS" BASIS, *
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14+
* See the License for the specific language governing permissions and *
15+
* limitations under the License. *
16+
***************************************************************************/
17+
18+
19+
#import <Foundation/Foundation.h>
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@class OptimizelyClient;
24+
25+
@interface SamplesForAPI: NSObject
26+
+(void)run:(OptimizelyClient*)optimizely;
27+
@end
28+
29+
NS_ASSUME_NONNULL_END

DemoObjCApp/Samples/SamplesForAPI.m

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
/****************************************************************************
3+
* Copyright 2019, Optimizely, Inc. and contributors *
4+
* *
5+
* Licensed under the Apache License, Version 2.0 (the "License"); *
6+
* you may not use this file except in compliance with the License. *
7+
* You may obtain a copy of the License at *
8+
* *
9+
* http://www.apache.org/licenses/LICENSE-2.0 *
10+
* *
11+
* Unless required by applicable law or agreed to in writing, software *
12+
* distributed under the License is distributed on an "AS IS" BASIS, *
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14+
* See the License for the specific language governing permissions and *
15+
* limitations under the License. *
16+
***************************************************************************/
17+
18+
19+
#import "SamplesForAPI.h"
20+
@import Optimizely;
21+
22+
@implementation SamplesForAPI
23+
24+
+(void)run:(OptimizelyClient*)optimizely {
25+
NSString *variationKey;
26+
27+
NSDictionary *attributes = @{
28+
@"device": @"iPhone",
29+
@"lifetime": @24738388,
30+
@"is_logged_in": @true
31+
};
32+
33+
NSDictionary *tags = @{
34+
@"category" : @"shoes",
35+
@"count": @5
36+
};
37+
38+
{
39+
NSString *variationKey = [optimizely activateWithExperimentKey:@"my_experiment_key"
40+
userId:@"user_123"
41+
attributes:attributes
42+
error:nil];
43+
NSLog(@"[activate] %@", variationKey);
44+
}
45+
{
46+
NSString *variationKey = [optimizely getVariationKeyWithExperimentKey:@"my_experiment_key"
47+
userId:@"user_123"
48+
attributes:attributes
49+
error:nil];
50+
NSLog(@"[getVariationKey] %@", variationKey);
51+
}
52+
{
53+
NSString *variationKey = [optimizely getForcedVariationWithExperimentKey:@"my_experiment_key"
54+
userId:@"user_123"];
55+
NSLog(@"[getForcedVariation] %@", variationKey);
56+
}
57+
{
58+
BOOL result = [optimizely setForcedVariationWithExperimentKey:@"my_experiment_key"
59+
userId:@"user_123"
60+
variationKey:@"some_variation_key"];
61+
NSLog(@"[setForcedVariation] %d", result);
62+
}
63+
{
64+
NSNumber *enabled = [optimizely isFeatureEnabledWithFeatureKey:@"my_feature_key"
65+
userId:@"user_123"
66+
attributes:attributes
67+
error:nil];
68+
NSLog(@"[isFeatureEnabled] %@", enabled);
69+
}
70+
{
71+
NSNumber *featureVariableValue = [optimizely getFeatureVariableDoubleWithFeatureKey:@"my_feature_key"
72+
variableKey:@"double_variable_key"
73+
userId:@"user_123"
74+
attributes:attributes
75+
error:nil];
76+
NSLog(@"[getFeatureVariableDouble] %@", featureVariableValue);
77+
}
78+
{
79+
NSArray *enabledFeatures = [optimizely getEnabledFeaturesWithUserId:@"user_123"
80+
attributes:attributes
81+
error:nil];
82+
NSLog(@"[getEnabledFeatures] %@", enabledFeatures);
83+
}
84+
{
85+
[optimizely trackWithEventKey:@"my_purchase_event_key"
86+
userId:@"user_id"
87+
attributes:attributes
88+
eventTags:tags
89+
error:nil];
90+
NSLog(@"[track]");
91+
}
92+
93+
}
94+
95+
@end

DemoSwiftApp/DemoSwiftApp.xcodeproj/project.pbxproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
6EDBCE0122710E87009DF724 /* CustomLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EDBCDF522710E87009DF724 /* CustomLogger.swift */; };
3434
6EF2CF1D21E4158A00A5D78D /* tvOSMain.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6EF2CF1B21E4158A00A5D78D /* tvOSMain.storyboard */; };
3535
6EFA31F5227BB60E006FA572 /* SamplesForAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */; };
36+
6EFA3212227CD571006FA572 /* SamplesForAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */; };
3637
89E1F14CFCF4A1AA5AC0C8CB /* Pods_DemoSwiftiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B886F5E161647DB427A8FBC /* Pods_DemoSwiftiOS.framework */; };
3738
/* End PBXBuildFile section */
3839

@@ -286,6 +287,14 @@
286287
path = Resources;
287288
sourceTree = "<group>";
288289
};
290+
6EFA3229227CDDD3006FA572 /* Samples */ = {
291+
isa = PBXGroup;
292+
children = (
293+
6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */,
294+
);
295+
path = Samples;
296+
sourceTree = "<group>";
297+
};
289298
EA299ED51E5530A400931399 /* DemoSwifttvOS */ = {
290299
isa = PBXGroup;
291300
children = (
@@ -303,8 +312,8 @@
303312
EA299EE31E55313B00931399 /* FailureViewController.swift */,
304313
EA299EE41E55313B00931399 /* SplashScreenViewController.swift */,
305314
EA1E4AA51E5FD1C100BDDABD /* DemoSwiftApp-Bridging-Header.h */,
306-
6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */,
307315
6E96811A21EE9552009B7FEC /* Customization */,
316+
6EFA3229227CDDD3006FA572 /* Samples */,
308317
EA5246BF1DC7186800AF6685 /* DemoSwiftiOS */,
309318
EA299ED51E5530A400931399 /* DemoSwifttvOS */,
310319
6E96818C21EEA391009B7FEC /* SharedResources */,
@@ -650,6 +659,7 @@
650659
files = (
651660
25C6F3B921C95D1B00C2EBDD /* FailureViewController.swift in Sources */,
652661
25C6F3BD21C95D2100C2EBDD /* VariationViewController.swift in Sources */,
662+
6EFA3212227CD571006FA572 /* SamplesForAPI.swift in Sources */,
653663
6EDBCE0122710E87009DF724 /* CustomLogger.swift in Sources */,
654664
25C6F3B721C95D1800C2EBDD /* AppDelegate.swift in Sources */,
655665
25C6F3BA21C95D1E00C2EBDD /* SplashScreenViewController.swift in Sources */,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
/****************************************************************************
3+
* Copyright 2019, Optimizely, Inc. and contributors *
4+
* *
5+
* Licensed under the Apache License, Version 2.0 (the "License"); *
6+
* you may not use this file except in compliance with the License. *
7+
* You may obtain a copy of the License at *
8+
* *
9+
* http://www.apache.org/licenses/LICENSE-2.0 *
10+
* *
11+
* Unless required by applicable law or agreed to in writing, software *
12+
* distributed under the License is distributed on an "AS IS" BASIS, *
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14+
* See the License for the specific language governing permissions and *
15+
* limitations under the License. *
16+
***************************************************************************/
17+
18+
19+
import Foundation
20+
import Optimizely
21+
22+
class SamplesForAPI {
23+
24+
static func run(optimizely: OptimizelyClient) {
25+
26+
let attributes: [String: Any] = [
27+
"device": "iPhone",
28+
"lifetime": 24738388,
29+
"is_logged_in": true,
30+
]
31+
let eventTags: [String: Any] = [
32+
"category": "shoes",
33+
"count": 2,
34+
]
35+
36+
if let variationKey = try? optimizely.activate(experimentKey: "my_experiment_key",
37+
userId: "user_123",
38+
attributes: attributes) {
39+
print("[activate] \(variationKey)")
40+
}
41+
42+
if let variationKey = try? optimizely.getVariationKey(experimentKey: "my_experiment_key",
43+
userId: "user_123",
44+
attributes: attributes) {
45+
print("[getVariationKey] \(variationKey)")
46+
}
47+
48+
if let variationKey = optimizely.getForcedVariation(experimentKey: "my_experiment_key", userId: "user_123") {
49+
print("[getForcedVariation] \(variationKey)")
50+
}
51+
52+
if optimizely.setForcedVariation(experimentKey: "my_experiment_key",
53+
userId: "user_123",
54+
variationKey: "some_variation_key") {
55+
print("[setForcedVariation]")
56+
}
57+
58+
do {
59+
let enabled = try optimizely.isFeatureEnabled(featureKey: "my_feature_key",
60+
userId: "user_123",
61+
attributes: attributes)
62+
print("[isFeatureEnabled] \(enabled)")
63+
} catch {
64+
// error
65+
}
66+
67+
if let featureVariableValue = try? optimizely.getFeatureVariableDouble(featureKey: "my_feature_key",
68+
variableKey: "double_variable_key",
69+
userId: "user_123",
70+
attributes: attributes) {
71+
print("[getFeatureVariableDouble] \(featureVariableValue)")
72+
}
73+
74+
if let enabledFeatures = try? optimizely.getEnabledFeatures(userId: "user_123", attributes: attributes) {
75+
print("[getEnabledFeatures] \(enabledFeatures)")
76+
}
77+
78+
try? optimizely.track(eventKey: "my_purchase_event_key", userId: "user_123", attributes: attributes, eventTags: eventTags)
79+
print("[track]")
80+
}
81+
82+
83+
}

0 commit comments

Comments
 (0)