@@ -198,5 +198,72 @@ class SamplesForAPI {
198198 }
199199
200200 }
201+
202+ // MARK: - OptimizelyConfig
203+
204+ static func samplesForInitialization( ) {
205+
206+ // These are sample codes for synchronous and asynchronous SDK initializations with multiple options
207+
208+ guard let localDatafileUrl = Bundle . main. url ( forResource: " demoTestDatafile " , withExtension: " json " ) ,
209+ let localDatafile = try ? Data ( contentsOf: localDatafileUrl)
210+ else {
211+ fatalError ( " Local datafile cannot be found " )
212+ }
213+
214+ var optimizely : OptimizelyClient
215+ var variationKey : String ?
216+
217+ // [Synchronous]
218+
219+ // [S1] Synchronous initialization
220+ // 1. SDK is initialized instantly with a cached (or bundled) datafile
221+ // 2. A new datafile can be downloaded in background and cached after the SDK is initialized.
222+ // The cached datafile will be used only when the SDK re-starts in the next session.
223+ optimizely = OptimizelyClient ( sdkKey: " <Your_SDK_Key> " )
224+ try ? optimizely. start ( datafile: localDatafile)
225+ variationKey = try ? optimizely. activate ( experimentKey: " <Experiment_Key " , userId: " <User_ID> " )
226+
227+ // [S2] Synchronous initialization
228+ // 1. SDK is initialized instantly with a cached (or bundled) datafile
229+ // 2. A new datafile can be downloaded in background and cached after the SDK is initialized.
230+ // The cached datafile is used immediately to update the SDK project config.
231+ optimizely = OptimizelyClient ( sdkKey: " <Your_SDK_Key> " )
232+ try ? optimizely. start ( datafile: localDatafile,
233+ doUpdateConfigOnNewDatafile: true )
234+ variationKey = try ? optimizely. activate ( experimentKey: " <Experiment_Key " , userId: " <User_ID> " )
235+
236+ // [S3] Synchronous initialization
237+ // 1. SDK is initialized instantly with a cached (or bundled) datafile
238+ // 2. A new datafile can be downloaded in background and cached after the SDK is initialized.
239+ // The cached datafile is used immediately to update the SDK project config.
240+ // 3. Polling datafile periodically.
241+ // The cached datafile is used immediately to update the SDK project config.
242+ optimizely = OptimizelyClient ( sdkKey: " <Your_SDK_Key> " ,
243+ periodicDownloadInterval: 60 )
244+ try ? optimizely. start ( datafile: localDatafile)
245+ variationKey = try ? optimizely. activate ( experimentKey: " <Experiment_Key " , userId: " <User_ID> " )
246+
247+ // [Asynchronous]
248+
249+ // [A1] Asynchronous initialization
250+ // 1. A datafile is downloaded from the server and the SDK is initialized with the datafile
251+ optimizely = OptimizelyClient ( sdkKey: " <Your_SDK_Key> " )
252+ optimizely. start { result in
253+ variationKey = try ? optimizely. activate ( experimentKey: " <Experiment_Key " , userId: " <User_ID> " )
254+ }
255+
256+ // [A2] Asynchronous initialization
257+ // 1. A datafile is downloaded from the server and the SDK is initialized with the datafile
258+ // 2. Polling datafile periodically.
259+ // The cached datafile is used immediately to update the SDK project config.
260+ optimizely = OptimizelyClient ( sdkKey: " <Your_SDK_Key> " ,
261+ periodicDownloadInterval: 60 )
262+ optimizely. start { result in
263+ variationKey = try ? optimizely. activate ( experimentKey: " <Experiment_Key " , userId: " <User_ID> " )
264+ }
265+
266+ print ( " activated variation: \( String ( describing: variationKey) ) " )
267+ }
201268
202269}
0 commit comments