@@ -24,7 +24,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
24
24
let experimentKey = " background_experiment "
25
25
let eventKey = " sample_conversion "
26
26
27
- let userId = String ( Int . random ( in: 0 ..< 300000 ) )
27
+ let userId = String ( Int . random ( in: 0 ..< 100000 ) )
28
28
let attributes : [ String : Any ? ] = [ " browser_type " : " safari " , " bool_attr " : false ]
29
29
30
30
var window : UIWindow ?
@@ -50,48 +50,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
50
50
initializeOptimizelySDKAsynchronous ( )
51
51
}
52
52
53
+ // MARK: - Initialization Examples
54
+
53
55
func initializeOptimizelySDKAsynchronous( ) {
56
+ optimizely = OptimizelyManager ( sdkKey: sdkKey)
54
57
55
- // customization example (optional)
56
- let customLogger = makeCustomLogger ( )
57
-
58
- optimizely = OptimizelyManager ( sdkKey: sdkKey,
59
- logger: customLogger,
60
- periodicDownloadInterval: 30 )
61
-
62
- _ = optimizely? . notificationCenter. addDatafileChangeNotificationListener ( datafileListener: { ( data) in
63
- DispatchQueue . main. async {
64
- #if os(iOS)
65
- let alert = UIAlertView ( title: " Datafile change " , message: " something changed. " , delegate: nil , cancelButtonTitle: " cancel " )
66
- alert. show ( )
67
- #else
68
- print ( " Datafile changed " )
69
- #endif
70
- }
71
- if let controller = self . window? . rootViewController as? VariationViewController {
72
- //controller.showCoupon = toggle == FeatureFlagToggle.on ? true : false;
73
- if let showCoupon = try ? self . optimizely? . isFeatureEnabled ( featureKey: " show_coupon " , userId: self . userId) {
74
- controller. showCoupon = showCoupon
75
- }
76
-
77
- }
78
- } )
79
-
80
- _ = optimizely? . notificationCenter. addActivateNotificationListener ( activateListener: { ( experiment, userId, attributes, variation, event) in
81
- print ( " got activate notification " )
82
- } )
83
-
84
- _ = optimizely? . notificationCenter. addTrackNotificationListener ( trackListener: { ( eventKey, userId, attributes, eventTags, event) in
85
- print ( eventKey)
86
- print ( userId)
87
- print ( attributes)
88
- print ( eventTags)
89
- print ( event)
90
- print ( " got track notification " )
91
- } )
92
-
93
- // initialize Optimizely Client from a datafile download
94
- optimizely!. initializeSDK { result in
58
+ optimizely. initializeSDK { result in
95
59
switch result {
96
60
case . failure( let error) :
97
61
print ( " Optimizely SDK initiliazation failed: \( error) " )
@@ -101,21 +65,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
101
65
}
102
66
103
67
DispatchQueue . main. async {
104
- self . setRootViewController ( optimizelyManager : self . optimizely )
68
+ self . setRootViewController ( )
105
69
}
106
70
}
107
71
}
108
72
109
73
func initializeOptimizelySDKSynchronous( ) {
110
- guard let localDatafilePath = Bundle ( for : self . classForCoder ) . path ( forResource: datafileName, ofType: " json " ) else {
74
+ guard let localDatafilePath = Bundle . main . path ( forResource: datafileName, ofType: " json " ) else {
111
75
fatalError ( " Local datafile cannot be found " )
112
76
}
113
77
114
- // customization example (optional)
115
- let customLogger = makeCustomLogger ( )
116
-
117
- optimizely = OptimizelyManager ( sdkKey: sdkKey,
118
- logger: customLogger)
78
+ optimizely = OptimizelyManager ( sdkKey: sdkKey)
119
79
120
80
do {
121
81
let datafileJSON = try String ( contentsOfFile: localDatafilePath, encoding: . utf8)
@@ -126,103 +86,84 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
126
86
optimizely = nil
127
87
}
128
88
129
- setRootViewController ( optimizelyManager : self . optimizely )
89
+ setRootViewController ( )
130
90
}
131
91
132
- func setRootViewController( optimizelyManager: OptimizelyManager ? ) {
133
- guard let optimizely = optimizely else {
134
- openFailureView ( )
135
- return
136
- }
92
+ func initializeOptimizelySDKWithCustomization( ) {
93
+ // customization example (optional)
94
+
95
+ let customLogger = CustomLogger ( )
96
+ let customDownloadIntervalInSecs = 30
97
+
98
+ optimizely = OptimizelyManager ( sdkKey: sdkKey,
99
+ logger: customLogger,
100
+ periodicDownloadInterval: customDownloadIntervalInSecs)
101
+
102
+ // notification listeners
103
+
104
+ _ = optimizely? . notificationCenter. addDecisionNotificationListener ( decisionListener: { ( type, userId, attributes, decisionInfo) in
105
+ print ( " Received decision notification: \( type) \( userId) \( String ( describing: attributes) ) \( decisionInfo) " )
106
+ } )
107
+
108
+ _ = optimizely? . notificationCenter. addTrackNotificationListener ( trackListener: { ( eventKey, userId, attributes, eventTags, event) in
109
+ print ( " Received track notification: \( eventKey) \( userId) \( String ( describing: attributes) ) \( String ( describing: eventTags) ) \( event) " )
110
+ } )
111
+
112
+ // initialize SDK
137
113
114
+ optimizely!. initializeSDK { result in
115
+ switch result {
116
+ case . failure( let error) :
117
+ print ( " Optimizely SDK initiliazation failed: \( error) " )
118
+ self . optimizely = nil
119
+ case . success:
120
+ print ( " Optimizely SDK initialized successfully! " )
121
+ }
122
+
123
+ DispatchQueue . main. async {
124
+ self . setRootViewController ( )
125
+ }
126
+ }
127
+ }
128
+
129
+ // MARK: - ViewControl
130
+
131
+ func setRootViewController( ) {
138
132
do {
139
133
let variationKey = try optimizely. activate ( experimentKey: experimentKey,
140
134
userId: userId,
141
135
attributes: attributes)
142
- openVariationView ( optimizelyManager: optimizely, variationKey: variationKey)
143
- // used to test threading and datafile updates.
144
- //
145
- // DispatchQueue.global(qos: .background).async {
146
- // repeat {
147
- // do {
148
- // let userId = String(Int(arc4random_uniform(300000)))
149
- // let variationKey = try optimizely.activate(experimentKey: self.experimentKey,
150
- // userId: userId,
151
- // attributes: self.attributes)
152
- // print(variationKey)
153
- // }
154
- // catch let error {
155
- // print(error)
156
- // }
157
- // sleep(1)
158
- // }
159
- // while true
160
- //
161
- // }
136
+ openVariationView ( variationKey: variationKey)
162
137
} catch OptimizelyError . variationUnknown( userId, experimentKey) {
163
138
print ( " Optimizely SDK activation cannot map this user to experiemnt " )
164
- openVariationView ( optimizelyManager : optimizely , variationKey: nil )
139
+ openVariationView ( variationKey: nil )
165
140
} catch {
166
141
print ( " Optimizely SDK activation failed: \( error) " )
167
142
openFailureView ( )
168
143
}
169
144
}
170
145
171
-
172
- func makeCustomLogger( ) -> OPTLogger {
173
- class Logger : OPTLogger {
174
- static var level : OptimizelyLogLevel ?
175
- static var logLevel : OptimizelyLogLevel {
176
- get {
177
- if let level = level {
178
- return level
179
- }
180
- return . all
181
- }
182
- set {
183
- if let _ = level {
184
- // already set.
185
- }
186
- else {
187
- level = newValue
188
- }
189
- }
190
- }
191
-
192
- required init ( ) {
193
-
194
- }
195
-
196
- func log( level: OptimizelyLogLevel , message: String ) {
197
- if level. rawValue <= Logger . logLevel. rawValue {
198
- print ( " 🐱 - [ \( level. name) ] Kitty - \( message) " )
199
- }
200
- }
201
-
202
- }
203
-
204
- return Logger ( )
205
- }
206
-
207
- func openVariationView( optimizelyManager: OptimizelyManager ? , variationKey: String ? ) {
146
+ func openVariationView( variationKey: String ? ) {
208
147
let variationViewController = storyboard. instantiateViewController ( withIdentifier: " VariationViewController " ) as! VariationViewController
209
148
210
- if let showCoupon = try ? optimizelyManager ? . isFeatureEnabled ( featureKey: " show_coupon " , userId: self . userId) {
149
+ if let showCoupon = try ? optimizely . isFeatureEnabled ( featureKey: " show_coupon " , userId: userId) {
211
150
variationViewController. showCoupon = showCoupon
212
151
}
213
152
214
- variationViewController. eventKey = eventKey
153
+ variationViewController. optimizelyManager = optimizely
215
154
variationViewController. userId = userId
216
- variationViewController. optimizelyManager = optimizelyManager
217
155
variationViewController. variationKey = variationKey
218
-
156
+ variationViewController. eventKey = eventKey
157
+
219
158
window? . rootViewController = variationViewController
220
159
}
221
-
160
+
222
161
func openFailureView( ) {
223
162
window? . rootViewController = storyboard. instantiateViewController ( withIdentifier: " FailureViewController " )
224
163
}
225
164
165
+ // MARK: - AppDelegate
166
+
226
167
func applicationWillResignActive( _ application: UIApplication ) {
227
168
}
228
169
0 commit comments