-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathAppDelegate.m
227 lines (177 loc) · 9.62 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Copyright 2019-2021, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "AppDelegate.h"
#import "VariationViewController.h"
#import "CustomLogger.h"
#import "SamplesForAPI.h"
@import Optimizely;
static NSString * const kOptimizelySdkKey = @"FCnSegiEkRry9rhVMroit4";
static NSString * const kOptimizelyDatafileName = @"demoTestDatafile";
static NSString * const kOptimizelyFeatureKey = @"decide_demo";
static NSString * const kOptimizelyExperimentKey = @"background_experiment_decide";
static NSString * const kOptimizelyEventKey = @"sample_conversion";
@interface AppDelegate ()
@property(nonnull, strong, nonatomic) NSString *userId;
@property(nonnull, strong, nonatomic) NSDictionary *attributes;
@property(nullable, strong, nonatomic) OptimizelyClient *optimizely;
@property(nullable, strong, nonatomic) OptimizelyUserContext *user;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.userId = [NSString stringWithFormat:@"%d", arc4random_uniform(300000)];
self.attributes = @{ @"location": @"CA", @"semanticVersioning": @"1.2"};
// initialize SDK in one of these two ways:
// (1) asynchronous SDK initialization (RECOMMENDED)
// - fetch a JSON datafile from the server
// - network delay, but the local configuration is in sync with the server experiment settings
// (2) synchronous SDK initialization
// - initialize immediately with the given JSON datafile or its cached copy
// - no network delay, but the local copy is not guaranteed to be in sync with the server experiment settings
[self initializeOptimizelySDKWithCustomization];
return YES;
}
// MARK: - Initialization Examples
-(void)initializeOptimizelySDKAsynchronous {
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey
logger:nil
eventDispatcher:nil
userProfileService:nil
periodicDownloadInterval:@(5)
defaultLogLevel:OptimizelyLogLevelDebug];
[self.optimizely startWithCompletion:^(NSData *data, NSError *error) {
if (error == nil) {
NSLog(@"Optimizely SDK initialized successfully!");
} else {
NSLog(@"Optimizely SDK initiliazation failed: %@", error.localizedDescription);
}
[self startWithRootViewController];
}];
}
-(void)initializeOptimizelySDKSynchronous {
NSString *localDatafilePath = [[NSBundle mainBundle] pathForResource:kOptimizelyDatafileName ofType:@"json"];
if (localDatafilePath == nil) {
NSAssert(false, @"Local datafile cannot be found");
return;
}
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey];
NSString *datafileJSON = [NSString stringWithContentsOfFile:localDatafilePath encoding:NSUTF8StringEncoding error:nil];
if (datafileJSON == nil) {
NSLog(@"Invalid JSON format");
} else {
NSError *error;
BOOL status = [self.optimizely startWithDatafile:datafileJSON error:&error];
if (status) {
NSLog(@"Optimizely SDK initialized successfully!");
} else {
NSLog(@"Optimizely SDK initiliazation failed: %@", error.localizedDescription);
}
}
[self startWithRootViewController];
}
-(void)initializeOptimizelySDKWithCustomization {
// customization example (optional)
// You can enable background datafile polling by setting periodicDownloadInterval (polling is disabled by default)
// 60 sec interval may be too frequent. This is for demo purpose. (You can set this to nil to use the recommended value of 600 secs).
NSNumber *downloadIntervalInSecs = @(60);
// You can turn off event batching with 0 timerInterval (this means that events are sent out immediately to the server instead of saving in the local queue for batching)
DefaultEventDispatcher *eventDispatcher = [[DefaultEventDispatcher alloc] initWithBatchSize:10
timerInterval:0
maxQueueSize:1000];
// customize logger
CustomLogger *customLogger = [[CustomLogger alloc] init];
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey
logger:customLogger
eventDispatcher:eventDispatcher
userProfileService:nil
periodicDownloadInterval:downloadIntervalInSecs
defaultLogLevel:OptimizelyLogLevelDebug];
[self addNotificationListeners];
[self.optimizely startWithCompletion:^(NSData *data, NSError *error) {
if (error == nil) {
NSLog(@"Optimizely SDK initialized successfully!");
} else {
NSLog(@"Optimizely SDK initiliazation failed: %@", error.localizedDescription);
}
[self startWithRootViewController];
// For sample codes for APIs, see "Samples/SamplesForAPI.swift"
//[SamplesForAPI checkOptimizelyConfig:self.optimizely];
//[SamplesForAPI checkOptimizelyUserContext:self.optimizely];
}];
}
-(void)addNotificationListeners {
NSNumber *notifId;
notifId = [self.optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener:^(NSString *type,
NSString *userId,
NSDictionary<NSString *,id> *attributes,
NSDictionary<NSString *,id> *decisionInfo) {
NSLog(@"Received decision notification: %@ %@ %@ %@", type, userId, attributes, decisionInfo);
}];
notifId = [self.optimizely.notificationCenter addTrackNotificationListenerWithTrackListener:^(NSString *eventKey,
NSString *userId,
NSDictionary<NSString *,id> *attributes, NSDictionary<NSString *,id> *eventTags, NSDictionary<NSString *,id> *event) {
NSLog(@"Received track notification: %@ %@ %@ %@ %@", eventKey, userId, attributes, eventTags, event);
}];
notifId = [self.optimizely.notificationCenter addLogEventNotificationListenerWithLogEventListener:^(NSString *url,
NSDictionary<NSString *,id> *event) {
NSLog(@"Received logEvent notification: %@ %@", url, event);
}];
}
// MARK: - ViewControl
-(void)startWithRootViewController {
dispatch_async(dispatch_get_main_queue(), ^{
// For sample codes for other APIs, see "Samples/SamplesForAPI.m"
self.user = [self.optimizely createUserContextWithUserId:self.userId
attributes:self.attributes];
OptimizelyDecision *decision = [self.user decideWithKey:kOptimizelyFeatureKey
options:@[@(OptimizelyDecideOptionIncludeReasons)]];
if (decision.variationKey != nil) {
[self openVariationViewWithVariationKey:decision.variationKey];
} else {
NSLog(@"Optimizely SDK activation failed: %@", decision.reasons);
[self openFailureView];
}
});
}
-(void)openVariationViewWithVariationKey:(nullable NSString*)variationKey {
VariationViewController *variationViewController = [self.storyboard instantiateViewControllerWithIdentifier: @"VariationViewController"];
variationViewController.optimizely = self.optimizely;
variationViewController.userId = self.userId;
variationViewController.variationKey = variationKey;
variationViewController.eventKey = kOptimizelyEventKey;
self.window.rootViewController = variationViewController;
}
-(void)openFailureView {
self.window.rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FailureViewController"];
}
-(UIStoryboard*)storyboard {
#if TARGET_OS_IOS
return [UIStoryboard storyboardWithName:@"iOSMain" bundle:nil];
#else
return [UIStoryboard storyboardWithName:@"tvOSMain" bundle:nil];
#endif
}
// MARK: - AppDelegate
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end