Skip to content

Commit c9a4149

Browse files
authored
Merge pull request #174 from optimizely/jae/samples
add sample codes for Swift and ObjC apps
2 parents 4a22fb1 + 5b75ab9 commit c9a4149

File tree

7 files changed

+342
-17
lines changed

7 files changed

+342
-17
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "AppDelegate.h"
1818
#import "VariationViewController.h"
1919
#import "CustomLogger.h"
20+
#import "SamplesForAPI.h"
2021

2122
@import Optimizely;
2223
#if TARGET_OS_IOS
@@ -155,6 +156,8 @@ -(void)initializeOptimizelySDKWithCustomization {
155156

156157
-(void)startWithRootViewController {
157158
dispatch_async(dispatch_get_main_queue(), ^{
159+
// For sample codes for other APIs, see "Samples/SamplesForAPI.m"
160+
158161
NSError *error;
159162
NSString *variationKey = [self.optimizely activateWithExperimentKey:kOptimizelyExperimentKey
160163
userId:self.userId

DemoObjCApp/DemoObjcApp.xcodeproj/project.pbxproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
6E9681D921EFA5B2009B7FEC /* CustomLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E9681D721EFA5B2009B7FEC /* CustomLogger.m */; };
3131
6EB8456B21EE5DE3005346CC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EB8456421EE5DE3005346CC /* AppDelegate.m */; };
3232
6EB8456C21EE5DE3005346CC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EB8456421EE5DE3005346CC /* AppDelegate.m */; };
33+
6EFA3206227CD55E006FA572 /* SamplesForAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA3205227CD55E006FA572 /* SamplesForAPI.m */; };
34+
6EFA3207227CD55E006FA572 /* SamplesForAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA3205227CD55E006FA572 /* SamplesForAPI.m */; };
3335
A0C60F82A88EDB74234D7D3F /* Pods_DemoObjctvOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 48C9592909E0CF5074F9A84D /* Pods_DemoObjctvOS.framework */; };
3436
/* End PBXBuildFile section */
3537

@@ -181,6 +183,8 @@
181183
6EF7498E21E404BB008B22A0 /* DemoObjciOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoObjciOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
182184
6EF7499F21E404BC008B22A0 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
183185
6EF749A821E404D6008B22A0 /* DemoObjctvOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoObjctvOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
186+
6EFA3204227CD55E006FA572 /* SamplesForAPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SamplesForAPI.h; sourceTree = "<group>"; };
187+
6EFA3205227CD55E006FA572 /* SamplesForAPI.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SamplesForAPI.m; sourceTree = "<group>"; };
184188
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>"; };
185189
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>"; };
186190
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>"; };
@@ -300,6 +304,7 @@
300304
6EB8456621EE5DE3005346CC /* VariationViewController.h */,
301305
6EB8456521EE5DE3005346CC /* VariationViewController.m */,
302306
6E96812021EE957B009B7FEC /* Customization */,
307+
6EFA322A227CDDF0006FA572 /* Samples */,
303308
6E96817721EEA321009B7FEC /* DemoObjciOS */,
304309
6E96817321EEA321009B7FEC /* DemoObjctvOS */,
305310
6E9681A121EEA3D8009B7FEC /* SharedResources */,
@@ -319,6 +324,15 @@
319324
name = Products;
320325
sourceTree = "<group>";
321326
};
327+
6EFA322A227CDDF0006FA572 /* Samples */ = {
328+
isa = PBXGroup;
329+
children = (
330+
6EFA3204227CD55E006FA572 /* SamplesForAPI.h */,
331+
6EFA3205227CD55E006FA572 /* SamplesForAPI.m */,
332+
);
333+
path = Samples;
334+
sourceTree = "<group>";
335+
};
322336
E4F9C3F25D5A22985B6E41CD /* Frameworks */ = {
323337
isa = PBXGroup;
324338
children = (
@@ -628,6 +642,8 @@
628642
6E9681BA21EEB361009B7FEC /* VariationViewController.m in Sources */,
629643
6EB8456B21EE5DE3005346CC /* AppDelegate.m in Sources */,
630644
6E9681D821EFA5B2009B7FEC /* CustomLogger.m in Sources */,
645+
646+
6EFA3206227CD55E006FA572 /* SamplesForAPI.m in Sources */,
631647
6E4DD7F721E51E5500B0C2C7 /* main.m in Sources */,
632648
);
633649
runOnlyForDeploymentPostprocessing = 0;
@@ -639,6 +655,7 @@
639655
6E9681BD21EEB364009B7FEC /* VariationViewController.m in Sources */,
640656
6EB8456C21EE5DE3005346CC /* AppDelegate.m in Sources */,
641657
6E9681D921EFA5B2009B7FEC /* CustomLogger.m in Sources */,
658+
6EFA3207227CD55E006FA572 /* SamplesForAPI.m in Sources */,
642659
6E4DD7F821E51E5600B0C2C7 /* main.m in Sources */,
643660
);
644661
runOnlyForDeploymentPostprocessing = 0;

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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
NSDictionary *attributes = @{
26+
@"device": @"iPhone",
27+
@"lifetime": @24738388,
28+
@"is_logged_in": @true
29+
};
30+
31+
NSDictionary *tags = @{
32+
@"category" : @"shoes",
33+
@"count": @5
34+
};
35+
36+
// MARK: - activate
37+
38+
{
39+
NSError *error = nil;
40+
NSString *variationKey = [optimizely activateWithExperimentKey:@"my_experiment_key"
41+
userId:@"user_123"
42+
attributes:attributes
43+
error:&error];
44+
if (variationKey == nil) {
45+
NSLog(@"Error: %@", error);
46+
} else {
47+
NSLog(@"[activate] %@", variationKey);
48+
}
49+
}
50+
51+
// MARK: - getVariationKey
52+
53+
{
54+
NSError *error = nil;
55+
NSString *variationKey = [optimizely getVariationKeyWithExperimentKey:@"my_experiment_key"
56+
userId:@"user_123"
57+
attributes:attributes
58+
error:&error];
59+
if (variationKey == nil) {
60+
NSLog(@"Error: %@", error);
61+
} else {
62+
NSLog(@"[getVariationKey] %@", variationKey);
63+
}
64+
}
65+
66+
// MARK: - getForcedVariation
67+
68+
{
69+
NSString *variationKey = [optimizely getForcedVariationWithExperimentKey:@"my_experiment_key"
70+
userId:@"user_123"];
71+
NSLog(@"[getForcedVariation] %@", variationKey);
72+
}
73+
74+
// MARK: - setForcedVariation
75+
76+
{
77+
BOOL result = [optimizely setForcedVariationWithExperimentKey:@"my_experiment_key"
78+
userId:@"user_123"
79+
variationKey:@"some_variation_key"];
80+
NSLog(@"[setForcedVariation] %d", result);
81+
}
82+
83+
// MARK: - isFeatureEnabled
84+
85+
{
86+
NSError *error = nil;
87+
NSNumber *enabled = [optimizely isFeatureEnabledWithFeatureKey:@"my_feature_key"
88+
userId:@"user_123"
89+
attributes:attributes
90+
error:&error];
91+
92+
if (enabled == nil) {
93+
NSLog(@"Error: %@", error);
94+
} else {
95+
NSLog(@"[isFeatureEnabled] %d", enabled.boolValue);
96+
}
97+
}
98+
99+
// MARK: - getFeatureVariable
100+
101+
{
102+
NSError *error = nil;
103+
NSNumber *featureVariableValue = [optimizely getFeatureVariableDoubleWithFeatureKey:@"my_feature_key"
104+
variableKey:@"double_variable_key"
105+
userId:@"user_123"
106+
attributes:attributes
107+
error:&error];
108+
if (featureVariableValue == nil) {
109+
NSLog(@"Error: %@", error);
110+
} else {
111+
NSLog(@"[getFeatureVariableDouble] %@", featureVariableValue);
112+
}
113+
}
114+
115+
// MARK: - getEnabledFeatures
116+
117+
{
118+
NSError *error = nil;
119+
NSArray *enabledFeatures = [optimizely getEnabledFeaturesWithUserId:@"user_123"
120+
attributes:attributes
121+
error:&error];
122+
if (enabledFeatures == nil) {
123+
NSLog(@"Error: %@", error);
124+
} else {
125+
NSLog(@"[getEnabledFeatures] %@", enabledFeatures);
126+
}
127+
}
128+
129+
// MARK: - track
130+
131+
{
132+
NSError *error = nil;
133+
BOOL success = [optimizely trackWithEventKey:@"my_purchase_event_key"
134+
userId:@"user_123"
135+
attributes:attributes
136+
eventTags:tags
137+
error:&error];
138+
if (success == false) {
139+
NSLog(@"Error: %@", error);
140+
} else {
141+
NSLog(@"[track]");
142+
}
143+
}
144+
145+
}
146+
147+
@end

DemoSwiftApp/AppDelegate.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
6767
print("Optimizely SDK initialized successfully!")
6868
}
6969

70-
DispatchQueue.main.async {
71-
self.startWithRootViewController()
72-
}
70+
self.startWithRootViewController()
7371
}
7472
}
7573

@@ -159,26 +157,28 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
159157
print("Optimizely SDK initialized successfully!")
160158
}
161159

162-
DispatchQueue.main.async {
163-
self.startWithRootViewController()
164-
}
160+
self.startWithRootViewController()
165161
}
166162
}
167163

168164
// MARK: - ViewControl
169165

170166
func startWithRootViewController() {
171-
do {
172-
let variationKey = try optimizely.activate(experimentKey: experimentKey,
173-
userId: userId,
174-
attributes: attributes)
175-
openVariationView(variationKey: variationKey)
176-
} catch OptimizelyError.variationUnknown(userId, experimentKey) {
177-
print("Optimizely SDK activation cannot map this user to experiemnt")
178-
openVariationView(variationKey: nil)
179-
} catch {
180-
print("Optimizely SDK activation failed: \(error)")
181-
openFailureView()
167+
DispatchQueue.main.async {
168+
do {
169+
// For sample codes for other APIs, see "Samples/SamplesForAPI.swift"
170+
171+
let variationKey = try self.optimizely.activate(experimentKey: self.experimentKey,
172+
userId: self.userId,
173+
attributes: self.attributes)
174+
self.openVariationView(variationKey: variationKey)
175+
} catch OptimizelyError.variationUnknown(self.userId, self.experimentKey) {
176+
print("Optimizely SDK activation cannot map this user to experiemnt")
177+
self.openVariationView(variationKey: nil)
178+
} catch {
179+
print("Optimizely SDK activation failed: \(error)")
180+
self.openFailureView()
181+
}
182182
}
183183
}
184184

DemoSwiftApp/DemoSwiftApp.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
6EDBCE0022710E87009DF724 /* CustomLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EDBCDF522710E87009DF724 /* CustomLogger.swift */; };
2929
6EDBCE0122710E87009DF724 /* CustomLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EDBCDF522710E87009DF724 /* CustomLogger.swift */; };
3030
6EF2CF1D21E4158A00A5D78D /* tvOSMain.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6EF2CF1B21E4158A00A5D78D /* tvOSMain.storyboard */; };
31+
6EFA31F5227BB60E006FA572 /* SamplesForAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */; };
32+
6EFA3212227CD571006FA572 /* SamplesForAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */; };
3133
89E1F14CFCF4A1AA5AC0C8CB /* Pods_DemoSwiftiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B886F5E161647DB427A8FBC /* Pods_DemoSwiftiOS.framework */; };
3234
/* End PBXBuildFile section */
3335

@@ -169,6 +171,7 @@
169171
6E96819421EEA391009B7FEC /* ProximaNova-Regular.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "ProximaNova-Regular.otf"; sourceTree = "<group>"; };
170172
6EDBCDF522710E87009DF724 /* CustomLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomLogger.swift; sourceTree = "<group>"; };
171173
6EF2CF1C21E4158A00A5D78D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/tvOSMain.storyboard; sourceTree = "<group>"; };
174+
6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SamplesForAPI.swift; sourceTree = "<group>"; };
172175
9C185BE70FBE51067CFD104E /* Pods-DemoSwiftiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoSwiftiOS.release.xcconfig"; path = "../Pods/Target Support Files/Pods-DemoSwiftiOS/Pods-DemoSwiftiOS.release.xcconfig"; sourceTree = "<group>"; };
173176
B301A636D5072557B288E62C /* Pods-DemoSwifttvOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoSwifttvOS.release.xcconfig"; path = "../Pods/Target Support Files/Pods-DemoSwifttvOS/Pods-DemoSwifttvOS.release.xcconfig"; sourceTree = "<group>"; };
174177
CCC404BE4046CDA1D3A9C26B /* Pods_DemoSwifttvOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DemoSwifttvOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -277,6 +280,14 @@
277280
path = Resources;
278281
sourceTree = "<group>";
279282
};
283+
6EFA3229227CDDD3006FA572 /* Samples */ = {
284+
isa = PBXGroup;
285+
children = (
286+
6EFA31F4227BB60E006FA572 /* SamplesForAPI.swift */,
287+
);
288+
path = Samples;
289+
sourceTree = "<group>";
290+
};
280291
EA299ED51E5530A400931399 /* DemoSwifttvOS */ = {
281292
isa = PBXGroup;
282293
children = (
@@ -292,6 +303,7 @@
292303
EA299EE21E55313B00931399 /* AppDelegate.swift */,
293304
EA299EE51E55313B00931399 /* VariationViewController.swift */,
294305
6E96811A21EE9552009B7FEC /* Customization */,
306+
6EFA3229227CDDD3006FA572 /* Samples */,
295307
EA5246BF1DC7186800AF6685 /* DemoSwiftiOS */,
296308
EA299ED51E5530A400931399 /* DemoSwifttvOS */,
297309
6E96818C21EEA391009B7FEC /* SharedResources */,
@@ -625,6 +637,7 @@
625637
25C6F3BC21C95D2100C2EBDD /* VariationViewController.swift in Sources */,
626638
6EDBCE0022710E87009DF724 /* CustomLogger.swift in Sources */,
627639
25C6F3B621C95D1700C2EBDD /* AppDelegate.swift in Sources */,
640+
6EFA31F5227BB60E006FA572 /* SamplesForAPI.swift in Sources */,
628641
);
629642
runOnlyForDeploymentPostprocessing = 0;
630643
};
@@ -633,6 +646,7 @@
633646
buildActionMask = 2147483647;
634647
files = (
635648
25C6F3BD21C95D2100C2EBDD /* VariationViewController.swift in Sources */,
649+
6EFA3212227CD571006FA572 /* SamplesForAPI.swift in Sources */,
636650
6EDBCE0122710E87009DF724 /* CustomLogger.swift in Sources */,
637651
25C6F3B721C95D1800C2EBDD /* AppDelegate.swift in Sources */,
638652
);

0 commit comments

Comments
 (0)