@@ -20,18 +20,18 @@ import Optimizely
20
20
@UIApplicationMain
21
21
class AppDelegate : UIResponder , UIApplicationDelegate {
22
22
let logLevel = OptimizelyLogLevel . debug
23
-
23
+
24
24
let sdkKey = " FCnSegiEkRry9rhVMroit4 "
25
25
let datafileName = " demoTestDatafile "
26
26
let featureKey = " decide_demo "
27
27
let experimentKey = " background_experiment_decide "
28
28
let eventKey = " sample_conversion "
29
-
29
+
30
30
let userId = String ( Int . random ( in: 0 ..< 100000 ) )
31
31
let attributes : [ String : Any ] = [ " location " : " NY " ,
32
32
" bool_attr " : false ,
33
33
" semanticVersioning " : " 1.2 " ]
34
-
34
+
35
35
var window : UIWindow ?
36
36
var optimizely : OptimizelyClient !
37
37
var user : OptimizelyUserContext !
@@ -43,9 +43,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
43
43
return UIStoryboard ( name: " tvOSMain " , bundle: nil )
44
44
#endif
45
45
}
46
-
46
+
47
47
func applicationDidFinishLaunching( _ application: UIApplication ) {
48
-
48
+
49
49
// initialize SDK in one of these two ways:
50
50
// (1) asynchronous SDK initialization (RECOMMENDED)
51
51
// - fetch a JSON datafile from the server
@@ -56,14 +56,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
56
56
57
57
initializeOptimizelySDKWithCustomization ( )
58
58
}
59
-
59
+
60
60
// MARK: - Initialization Examples
61
-
61
+
62
62
func initializeOptimizelySDKAsynchronous( ) {
63
63
optimizely = OptimizelyClient ( sdkKey: sdkKey, defaultLogLevel: logLevel)
64
-
64
+
65
65
addNotificationListeners ( )
66
-
66
+
67
67
optimizely. start { result in
68
68
switch result {
69
69
case . failure( let error) :
@@ -73,20 +73,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
73
73
@unknown default :
74
74
print ( " Optimizely SDK initiliazation failed with unknown result " )
75
75
}
76
-
76
+
77
77
self . startWithRootViewController ( )
78
78
}
79
79
}
80
-
80
+
81
81
func initializeOptimizelySDKSynchronous( ) {
82
82
guard let localDatafilePath = Bundle . main. path ( forResource: datafileName, ofType: " json " ) else {
83
83
fatalError ( " Local datafile cannot be found " )
84
84
}
85
-
85
+
86
86
optimizely = OptimizelyClient ( sdkKey: sdkKey, defaultLogLevel: logLevel)
87
87
88
88
addNotificationListeners ( )
89
-
89
+
90
90
do {
91
91
let datafileJSON = try String ( contentsOfFile: localDatafilePath, encoding: . utf8)
92
92
try optimizely. start ( datafile: datafileJSON)
@@ -95,29 +95,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
95
95
} catch {
96
96
print ( " Optimizely SDK initiliazation failed: \( error) " )
97
97
}
98
-
98
+
99
99
startWithRootViewController ( )
100
100
}
101
-
101
+
102
102
func initializeOptimizelySDKWithCustomization( ) {
103
103
// customization example (optional)
104
-
104
+
105
105
// You can enable background datafile polling by setting periodicDownloadInterval (polling is disabled by default)
106
106
// 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).
107
107
let downloadIntervalInSecs : Int ? = 60
108
-
108
+
109
109
// 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)
110
110
let eventDispatcher = DefaultEventDispatcher ( timerInterval: 0 )
111
-
111
+
112
112
// customize logger
113
113
let customLogger = CustomLogger ( )
114
114
115
115
optimizely = OptimizelyClient ( sdkKey: sdkKey,
116
- logger: customLogger,
117
- eventDispatcher: eventDispatcher,
118
- periodicDownloadInterval: downloadIntervalInSecs,
119
- defaultLogLevel: logLevel)
120
-
116
+ logger: customLogger,
117
+ eventDispatcher: eventDispatcher,
118
+ periodicDownloadInterval: downloadIntervalInSecs,
119
+ defaultLogLevel: logLevel)
120
+
121
121
addNotificationListeners ( )
122
122
123
123
// initialize SDK
@@ -141,15 +141,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
141
141
func addNotificationListeners( ) {
142
142
// notification listeners
143
143
let notificationCenter = optimizely. notificationCenter!
144
-
144
+
145
145
_ = notificationCenter. addDecisionNotificationListener ( decisionListener: { ( type, userId, attributes, decisionInfo) in
146
146
print ( " Received decision notification: \( type) \( userId) \( String ( describing: attributes) ) \( decisionInfo) " )
147
147
} )
148
148
149
149
_ = notificationCenter. addTrackNotificationListener ( trackListener: { ( eventKey, userId, attributes, eventTags, event) in
150
150
print ( " Received track notification: \( eventKey) \( userId) \( String ( describing: attributes) ) \( String ( describing: eventTags) ) \( event) " )
151
151
} )
152
-
152
+
153
153
_ = notificationCenter. addDatafileChangeNotificationListener ( datafileListener: { _ in
154
154
DispatchQueue . main. async {
155
155
#if os(iOS)
@@ -161,7 +161,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
161
161
#else
162
162
print ( " Datafile changed " )
163
163
#endif
164
-
164
+
165
165
if let controller = self . window? . rootViewController as? VariationViewController {
166
166
let decision = self . user. decide ( key: " show_coupon " )
167
167
controller. showCoupon = decision. enabled
@@ -177,9 +177,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
177
177
print ( " Received logEvent notification: \( url) \( event) " )
178
178
} )
179
179
}
180
-
180
+
181
181
// MARK: - ViewControl
182
-
182
+
183
183
func startWithRootViewController( ) {
184
184
DispatchQueue . main. async {
185
185
self . user = self . optimizely. createUserContext ( userId: self . userId,
@@ -194,49 +194,49 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
194
194
}
195
195
}
196
196
}
197
-
197
+
198
198
func openVariationView( variationKey: String ? ) {
199
199
if let variationViewController = storyboard. instantiateViewController ( withIdentifier: " VariationViewController " )
200
200
as? VariationViewController {
201
-
201
+
202
202
let decision = self . user. decide ( key: " show_coupon " )
203
203
variationViewController. showCoupon = decision. enabled
204
-
204
+
205
205
variationViewController. optimizely = optimizely
206
206
variationViewController. userId = userId
207
207
variationViewController. variationKey = variationKey
208
208
variationViewController. eventKey = eventKey
209
-
209
+
210
210
window? . rootViewController = variationViewController
211
211
}
212
212
}
213
-
213
+
214
214
func openFailureView( ) {
215
215
window? . rootViewController = storyboard. instantiateViewController ( withIdentifier: " FailureViewController " )
216
216
}
217
-
217
+
218
218
// MARK: - AppDelegate
219
-
219
+
220
220
func applicationWillResignActive( _ application: UIApplication ) {
221
221
}
222
-
222
+
223
223
func applicationDidEnterBackground( _ application: UIApplication ) {
224
224
}
225
-
225
+
226
226
func applicationWillEnterForeground( _ application: UIApplication ) {
227
227
}
228
-
228
+
229
229
func applicationDidBecomeActive( _ application: UIApplication ) {
230
230
}
231
-
231
+
232
232
func applicationWillTerminate( _ application: UIApplication ) {
233
233
}
234
-
234
+
235
235
func application( _ application: UIApplication ,
236
236
performFetchWithCompletionHandler completionHandler: ( UIBackgroundFetchResult ) -> Void ) {
237
-
237
+
238
238
// add background fetch task here
239
-
239
+
240
240
completionHandler ( . newData)
241
241
}
242
242
}
0 commit comments