9
9
#import " AppDelegate.h"
10
10
#import " VariationViewController.h"
11
11
#import " FailureViewController.h"
12
+ #import " CustomLogger.h"
12
13
13
14
@import Optimizely;
14
15
16
+ static NSString * const kOptimizelySdkKey = @" FCnSegiEkRry9rhVMroit4" ;
15
17
static NSString * const kOptimizelyDatafileName = @" demoTestDatafile" ;
16
18
static NSString * const kOptimizelyExperimentKey = @" background_experiment" ;
17
19
static NSString * const kOptimizelyEventKey = @" sample_conversion" ;
18
- static NSString * const kOptimizelySdkKey = @" AqLkkcss3wRGUbftnKNgh2" ;
19
20
20
21
@interface AppDelegate ()
21
22
@property (nonnull , strong , nonatomic ) NSString *userId;
@@ -25,12 +26,9 @@ @interface AppDelegate ()
25
26
26
27
@implementation AppDelegate
27
28
28
-
29
29
- (BOOL )application : (UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions {
30
-
31
30
self.userId = [NSString stringWithFormat: @" %d " , arc4random ()];
32
- self.attributes = @{ @" browser_type" : @" safari" };
33
-
31
+ self.attributes = @{ @" browser_type" : @" safari" , @" bool_attr" : @(false ) };
34
32
35
33
// initialize SDK in one of these two ways:
36
34
// (1) asynchronous SDK initialization (RECOMMENDED)
@@ -39,29 +37,25 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
39
37
// (2) synchronous SDK initialization
40
38
// - initialize immediately with the given JSON datafile or its cached copy
41
39
// - no network delay, but the local copy is not guaranteed to be in sync with the server experiment settings
42
-
40
+
43
41
[self initializeOptimizelySDKAsynchronous ];
44
- // [self initializeOptimizelySDKSynchronous];
45
-
46
42
return YES ;
47
43
}
48
44
45
+ // MARK: - Initialization Examples
46
+
49
47
-(void )initializeOptimizelySDKAsynchronous {
50
48
self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey ];
51
-
52
- [self .optimizely initializeSDKWithCompletion : ^(NSData * _Nullable data, NSError * _Nullable error) {
49
+
50
+ [self .optimizely startSDKWithCompletion : ^(NSData *data, NSError *error) {
53
51
if (error == nil ) {
54
52
NSLog (@" Optimizely SDK initialized successfully!" );
55
53
} else {
56
54
NSLog (@" Optimizely SDK initiliazation failed: %@ " , error.localizedDescription );
57
55
self.optimizely = nil ;
58
56
}
59
57
60
- [self .optimizely.notificationCenter addActivateNotificationListenerWithActivateListener: ^(NSDictionary <NSString *,id > * _Nonnull experiment, NSString * _Nonnull userId, NSDictionary <NSString *,id > * _Nullable attributes, NSDictionary <NSString *,id > * _Nonnull variation, NSDictionary <NSString *,id > * _Nonnull event) {
61
- NSLog (@" got activate with experiment" );
62
- NSLog (@" %@ " , experiment[@" key" ]);
63
- }];
64
- [self startAppWithExperimentActivated ];
58
+ [self startWithRootViewController ];
65
59
}];
66
60
}
67
61
@@ -75,17 +69,14 @@ -(void)initializeOptimizelySDKSynchronous {
75
69
76
70
self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey ];
77
71
78
- // customization example (optional)
79
- // TODO: add cutomization for ObjC
80
-
81
72
NSString *datafileJSON = [NSString stringWithContentsOfFile: localDatafilePath encoding: NSUTF8StringEncoding error: nil ];
82
-
73
+
83
74
if (datafileJSON == nil ) {
84
75
NSLog (@" Invalid JSON format" );
85
76
self.optimizely = nil ;
86
77
} else {
87
78
NSError *error;
88
- BOOL status = [self .optimizely initializeSDKWithDatafile : datafileJSON error: &error];
79
+ BOOL status = [self .optimizely startSDKWithDatafile : datafileJSON error: &error];
89
80
if (status) {
90
81
NSLog (@" Optimizely SDK initialized successfully!" );
91
82
} else {
@@ -94,72 +85,107 @@ -(void)initializeOptimizelySDKSynchronous {
94
85
}
95
86
}
96
87
97
- [self startAppWithExperimentActivated ];
88
+ [self startWithRootViewController ];
98
89
}
99
-
100
- -(void )startAppWithExperimentActivated {
101
- NSError *error;
102
- NSString *variationKey = [self .optimizely activateWithExperimentKey: kOptimizelyExperimentKey
103
- userId: self .userId
104
- attributes: self .attributes
105
- error: &error];
106
-
107
- if (variationKey == nil ) {
108
- NSLog (@" Optimizely SDK activation failed: %@ " , error.localizedDescription );
109
- self.optimizely = nil ;
110
- }
111
90
112
-
113
- [self setRootViewControllerWithOtimizelyManager: self .optimizely bucketedVariation: variationKey];
91
+ -(void )initializeOptimizelySDKWithCustomization {
92
+ // customization example (optional)
93
+
94
+ CustomLogger *customLogger = [[CustomLogger alloc ] init ];
95
+ // 30 sec interval may be too frequent. This is for demo purpose.
96
+ // This should be should be much larger (default = 10 mins).
97
+ NSNumber *customDownloadIntervalInSecs = @(30 );
98
+
99
+ self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey
100
+ logger: customLogger
101
+ eventDispatcher: nil
102
+ userProfileService: nil
103
+ periodicDownloadInterval: customDownloadIntervalInSecs
104
+ defaultLogLevel: OptimizelyLogLevelInfo];
105
+
106
+ NSNumber *notifId;
107
+ notifId = [self .optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener: ^(NSString *type,
108
+ NSString *userId,
109
+ NSDictionary <NSString *,id > *attributes,
110
+ NSDictionary <NSString *,id > *decisionInfo) {
111
+ NSLog (@" Received decision notification: %@ %@ %@ %@ " , type, userId, attributes, decisionInfo);
112
+ }];
113
+
114
+ notifId = [self .optimizely.notificationCenter addTrackNotificationListenerWithTrackListener: ^(NSString *eventKey,
115
+ NSString *userId,
116
+ NSDictionary <NSString *,id > *attributes, NSDictionary <NSString *,id > *eventTags, NSDictionary <NSString *,id > *event) {
117
+ NSLog (@" Received track notification: %@ %@ %@ %@ %@ " , eventKey, userId, attributes, eventTags, event);
118
+ }];
119
+
120
+ [self .optimizely startSDKWithCompletion: ^(NSData *data, NSError *error) {
121
+ if (error == nil ) {
122
+ NSLog (@" Optimizely SDK initialized successfully!" );
123
+ } else {
124
+ NSLog (@" Optimizely SDK initiliazation failed: %@ " , error.localizedDescription );
125
+ self.optimizely = nil ;
126
+ }
127
+
128
+ [self startWithRootViewController ];
129
+ }];
114
130
}
115
131
116
- -(void )setRootViewControllerWithOtimizelyManager : (OptimizelyManager*)manager bucketedVariation : (NSString *)variationKey {
132
+ // MARK: - ViewControl
133
+
134
+ -(void )startWithRootViewController {
117
135
dispatch_async (dispatch_get_main_queue (), ^{
136
+ NSError *error;
137
+ NSString *variationKey = [self .optimizely activateWithExperimentKey: kOptimizelyExperimentKey
138
+ userId: self .userId
139
+ attributes: self .attributes
140
+ error: &error];
118
141
119
- #if TARGET_OS_IOS
120
- UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @" iOSMain" bundle: nil ];
121
- #else
122
- UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @" tvOSMain" bundle: nil ];
123
- #endif
124
- UIViewController *rootViewController;
125
-
126
- if ((manager != nil ) && (variationKey != nil )) {
127
- VariationViewController *vc = [storyboard instantiateViewControllerWithIdentifier: @" VariationViewController" ];
128
-
129
- vc.eventKey = kOptimizelyEventKey ;
130
- vc.optimizely = manager;
131
- vc.userId = self.userId ;
132
- vc.variationKey = variationKey;
133
-
134
- rootViewController = vc;
142
+ if (variationKey != nil ) {
143
+ [self openVariationViewWithVariationKey: variationKey];
135
144
} else {
136
- rootViewController = [storyboard instantiateViewControllerWithIdentifier: @" FailureViewController" ];
145
+ NSLog (@" Optimizely SDK activation failed: %@ " , error.localizedDescription );
146
+ [self openFailureView ];
137
147
}
138
-
139
- self.window .rootViewController = rootViewController;
140
148
});
141
149
}
142
150
151
+ -(void )openVariationViewWithVariationKey : (nullable NSString *)variationKey {
152
+ VariationViewController *variationViewController = [self .storyboard instantiateViewControllerWithIdentifier: @" VariationViewController" ];
153
+
154
+ variationViewController.optimizely = self.optimizely ;
155
+ variationViewController.userId = self.userId ;
156
+ variationViewController.variationKey = variationKey;
157
+ variationViewController.eventKey = kOptimizelyEventKey ;
158
+
159
+ self.window .rootViewController = variationViewController;
160
+ }
143
161
162
+ -(void )openFailureView {
163
+ self.window .rootViewController = [self .storyboard instantiateViewControllerWithIdentifier: @" FailureViewController" ];
164
+ }
144
165
145
- - (void )applicationWillResignActive : (UIApplication *)application {
166
+ -(UIStoryboard*)storyboard {
167
+ #if TARGET_OS_IOS
168
+ return [UIStoryboard storyboardWithName: @" iOSMain" bundle: nil ];
169
+ #else
170
+ return [UIStoryboard storyboardWithName: @" tvOSMain" bundle: nil ];
171
+ #endif
146
172
}
147
173
174
+ // MARK: - AppDelegate
148
175
149
- - (void )applicationDidEnterBackground : (UIApplication *)application {
176
+ - (void )applicationWillResignActive : (UIApplication *)application {
150
177
}
151
178
179
+ - (void )applicationDidEnterBackground : (UIApplication *)application {
180
+ }
152
181
153
182
- (void )applicationWillEnterForeground : (UIApplication *)application {
154
183
}
155
184
156
-
157
185
- (void )applicationDidBecomeActive : (UIApplication *)application {
158
186
}
159
187
160
-
161
188
- (void )applicationWillTerminate : (UIApplication *)application {
162
189
}
163
190
164
-
165
191
@end
0 commit comments