@@ -20,18 +20,18 @@ import Optimizely
2020@UIApplicationMain
2121class AppDelegate : UIResponder , UIApplicationDelegate {
2222 let logLevel = OptimizelyLogLevel . debug
23-
23+
2424 let sdkKey = " FCnSegiEkRry9rhVMroit4 "
2525 let datafileName = " demoTestDatafile "
2626 let featureKey = " decide_demo "
2727 let experimentKey = " background_experiment_decide "
2828 let eventKey = " sample_conversion "
29-
29+
3030 let userId = String ( Int . random ( in: 0 ..< 100000 ) )
3131 let attributes : [ String : Any ] = [ " location " : " NY " ,
3232 " bool_attr " : false ,
3333 " semanticVersioning " : " 1.2 " ]
34-
34+
3535 var window : UIWindow ?
3636 var optimizely : OptimizelyClient !
3737 var user : OptimizelyUserContext !
@@ -43,9 +43,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4343 return UIStoryboard ( name: " tvOSMain " , bundle: nil )
4444 #endif
4545 }
46-
46+
4747 func applicationDidFinishLaunching( _ application: UIApplication ) {
48-
48+
4949 // initialize SDK in one of these two ways:
5050 // (1) asynchronous SDK initialization (RECOMMENDED)
5151 // - fetch a JSON datafile from the server
@@ -56,14 +56,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5656
5757 initializeOptimizelySDKWithCustomization ( )
5858 }
59-
59+
6060 // MARK: - Initialization Examples
61-
61+
6262 func initializeOptimizelySDKAsynchronous( ) {
6363 optimizely = OptimizelyClient ( sdkKey: sdkKey, defaultLogLevel: logLevel)
64-
64+
6565 addNotificationListeners ( )
66-
66+
6767 optimizely. start { result in
6868 switch result {
6969 case . failure( let error) :
@@ -73,20 +73,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7373 @unknown default :
7474 print ( " Optimizely SDK initiliazation failed with unknown result " )
7575 }
76-
76+
7777 self . startWithRootViewController ( )
7878 }
7979 }
80-
80+
8181 func initializeOptimizelySDKSynchronous( ) {
8282 guard let localDatafilePath = Bundle . main. path ( forResource: datafileName, ofType: " json " ) else {
8383 fatalError ( " Local datafile cannot be found " )
8484 }
85-
85+
8686 optimizely = OptimizelyClient ( sdkKey: sdkKey, defaultLogLevel: logLevel)
8787
8888 addNotificationListeners ( )
89-
89+
9090 do {
9191 let datafileJSON = try String ( contentsOfFile: localDatafilePath, encoding: . utf8)
9292 try optimizely. start ( datafile: datafileJSON)
@@ -95,29 +95,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
9595 } catch {
9696 print ( " Optimizely SDK initiliazation failed: \( error) " )
9797 }
98-
98+
9999 startWithRootViewController ( )
100100 }
101-
101+
102102 func initializeOptimizelySDKWithCustomization( ) {
103103 // customization example (optional)
104-
104+
105105 // You can enable background datafile polling by setting periodicDownloadInterval (polling is disabled by default)
106106 // 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).
107107 let downloadIntervalInSecs : Int ? = 60
108-
108+
109109 // 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)
110110 let eventDispatcher = DefaultEventDispatcher ( timerInterval: 0 )
111-
111+
112112 // customize logger
113113 let customLogger = CustomLogger ( )
114114
115115 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+
121121 addNotificationListeners ( )
122122
123123 // initialize SDK
@@ -141,15 +141,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
141141 func addNotificationListeners( ) {
142142 // notification listeners
143143 let notificationCenter = optimizely. notificationCenter!
144-
144+
145145 _ = notificationCenter. addDecisionNotificationListener ( decisionListener: { ( type, userId, attributes, decisionInfo) in
146146 print ( " Received decision notification: \( type) \( userId) \( String ( describing: attributes) ) \( decisionInfo) " )
147147 } )
148148
149149 _ = notificationCenter. addTrackNotificationListener ( trackListener: { ( eventKey, userId, attributes, eventTags, event) in
150150 print ( " Received track notification: \( eventKey) \( userId) \( String ( describing: attributes) ) \( String ( describing: eventTags) ) \( event) " )
151151 } )
152-
152+
153153 _ = notificationCenter. addDatafileChangeNotificationListener ( datafileListener: { _ in
154154 DispatchQueue . main. async {
155155 #if os(iOS)
@@ -161,7 +161,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
161161 #else
162162 print ( " Datafile changed " )
163163 #endif
164-
164+
165165 if let controller = self . window? . rootViewController as? VariationViewController {
166166 let decision = self . user. decide ( key: " show_coupon " )
167167 controller. showCoupon = decision. enabled
@@ -177,9 +177,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
177177 print ( " Received logEvent notification: \( url) \( event) " )
178178 } )
179179 }
180-
180+
181181 // MARK: - ViewControl
182-
182+
183183 func startWithRootViewController( ) {
184184 DispatchQueue . main. async {
185185 self . user = self . optimizely. createUserContext ( userId: self . userId,
@@ -194,49 +194,49 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
194194 }
195195 }
196196 }
197-
197+
198198 func openVariationView( variationKey: String ? ) {
199199 if let variationViewController = storyboard. instantiateViewController ( withIdentifier: " VariationViewController " )
200200 as? VariationViewController {
201-
201+
202202 let decision = self . user. decide ( key: " show_coupon " )
203203 variationViewController. showCoupon = decision. enabled
204-
204+
205205 variationViewController. optimizely = optimizely
206206 variationViewController. userId = userId
207207 variationViewController. variationKey = variationKey
208208 variationViewController. eventKey = eventKey
209-
209+
210210 window? . rootViewController = variationViewController
211211 }
212212 }
213-
213+
214214 func openFailureView( ) {
215215 window? . rootViewController = storyboard. instantiateViewController ( withIdentifier: " FailureViewController " )
216216 }
217-
217+
218218 // MARK: - AppDelegate
219-
219+
220220 func applicationWillResignActive( _ application: UIApplication ) {
221221 }
222-
222+
223223 func applicationDidEnterBackground( _ application: UIApplication ) {
224224 }
225-
225+
226226 func applicationWillEnterForeground( _ application: UIApplication ) {
227227 }
228-
228+
229229 func applicationDidBecomeActive( _ application: UIApplication ) {
230230 }
231-
231+
232232 func applicationWillTerminate( _ application: UIApplication ) {
233233 }
234-
234+
235235 func application( _ application: UIApplication ,
236236 performFetchWithCompletionHandler completionHandler: ( UIBackgroundFetchResult ) -> Void ) {
237-
237+
238238 // add background fetch task here
239-
239+
240240 completionHandler ( . newData)
241241 }
242242}
0 commit comments