Skip to content

Commit 9375b0a

Browse files
committed
use manager and client architecture in iOS demo app
1 parent 002e52b commit 9375b0a

File tree

1 file changed

+44
-91
lines changed

1 file changed

+44
-91
lines changed

OptimizelyiOSDemoApp/OptimizelyiOSDemoApp/AppDelegate.swift

Lines changed: 44 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,58 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3838

3939
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
4040

41+
// get the datafile
42+
let bundle = Bundle.init(for: self.classForCoder)
43+
let filePath = bundle.path(forResource: datafileName, ofType: "json")
44+
var jsonDatafile: Data? = nil
45+
do {
46+
let fileContents = try String.init(contentsOfFile: filePath!, encoding: String.Encoding.utf8)
47+
jsonDatafile = fileContents.data(using: String.Encoding.utf8)!
48+
}
49+
catch {
50+
print("invalid JSON Data")
51+
}
52+
53+
// create the event dispatcher
54+
let eventDispatcher = OPTLYEventDispatcher.initWithBuilderBlock{(builder)in
55+
builder?.eventDispatcherDispatchInterval = self.eventDispatcherDispatchInterval
56+
}
57+
58+
// create the datafile manager
59+
let datafileManager = OPTLYDatafileManager.initWithBuilderBlock{(builder) in
60+
builder!.datafileFetchInterval = TimeInterval(self.datafileManagerDownloadInterval)
61+
builder!.projectId = self.projectId
62+
}
63+
64+
let optimizelyManager = OPTLYManager.initWithBuilderBlock {(builder) in
65+
builder!.datafile = jsonDatafile
66+
builder!.projectId = self.projectId
67+
builder!.datafileManager = datafileManager!
68+
builder!.eventDispatcher = eventDispatcher
69+
}
70+
4171
// use different parameters if initializing Optimizely from downloaded datafile
4272
if self.downloadDatafile == true {
43-
projectId = "7791451651"
4473
attributes = ["userType" : "new"]
4574
eventKey = "userEvent"
4675
experimentKey = "exp1"
4776

48-
// initialize Optimizely from a datafile download
49-
initializeOptimizely(projectId:projectId, completionHandler: { (optimizely) in
50-
let OPTLYVariation = optimizely?.activateExperiment(self.experimentKey, userId: self.userId, attributes: self.attributes)
51-
print("bucketed variation:", OPTLYVariation)
52-
optimizely?.trackEvent(self.eventKey, userId: self.userId, attributes: self.attributes, eventValue: self.revenue)
77+
// initialize Optimizely Client from a datafile download
78+
optimizelyManager?.initializeClient(callback: { (error, optimizelyClient) in
79+
let variation = optimizelyClient?.activateExperiment(self.experimentKey, userId: self.userId, attributes: self.attributes)
80+
if (variation != nil) {
81+
print("bucketed variation:", variation!.variationKey)
82+
}
83+
optimizelyClient?.trackEvent(self.eventKey, userId: self.userId, attributes: self.attributes, eventValue: self.revenue)
5384
})
5485
} else {
55-
// initialize Optimizely from a saved datafile
56-
let optimizely = initializeOptimizely(datafileName: datafileName)
57-
optimizely.activateExperiment(self.experimentKey, userId: self.userId, attributes: self.attributes)
58-
optimizely.trackEvent(self.eventKey, userId: self.userId, attributes: self.attributes, eventValue: self.revenue)
86+
// initialize Optimizely Client from a saved datafile
87+
let optimizelyClient = optimizelyManager?.initializeClient()
88+
let variation = optimizelyClient?.activateExperiment(self.experimentKey, userId: self.userId, attributes: self.attributes)
89+
if (variation != nil) {
90+
print("bucketed variation:", variation!.variationKey)
91+
}
92+
optimizelyClient?.trackEvent(self.eventKey, userId: self.userId, attributes: self.attributes, eventValue: self.revenue)
5993
}
6094

6195

@@ -83,86 +117,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
83117
func applicationWillTerminate(_ application: UIApplication) {
84118
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
85119
}
86-
87-
// --- initialize Optimizely from a saved datafile ----
88-
func initializeOptimizely(datafileName:String) -> Optimizely {
89-
90-
var optimizely : Optimizely? = nil
91-
let bundle = Bundle.init(for: self.classForCoder)
92-
if let filePath = bundle.path(forResource: datafileName, ofType: "json") {
93-
do {
94-
let fileContents = try String.init(contentsOfFile: filePath, encoding: String.Encoding.utf8)
95-
let jsonData = fileContents.data(using: String.Encoding.utf8)!
96-
print(fileContents)
97-
98-
let projectConfig = OPTLYProjectConfig.initWithBuilderBlock({ (builder) in
99-
builder?.datafile = jsonData
100-
builder?.userProfile = OPTLYUserProfile.init()
101-
});
102-
print("projectConfig: ", projectConfig)
103-
104-
// create the event dispatcher
105-
let eventDispatcherBuilderBlock : OPTLYEventDispatcherBuilderBlock = {(builder)in
106-
builder?.eventDispatcherDispatchInterval = self.eventDispatcherDispatchInterval
107-
}
108-
let eventDispatcher = OPTLYEventDispatcher.initWithBuilderBlock(eventDispatcherBuilderBlock)
109-
110-
// create the datafile manager
111-
let datafileBuilderBlock : OPTLYDatafileManagerBuilderBlock = {[weak self] (builder) in
112-
builder?.datafileFetchInterval = TimeInterval(self!.datafileManagerDownloadInterval)
113-
builder?.projectId = self!.projectId
114-
}
115-
let datafileManager = OPTLYDatafileManager.initWithBuilderBlock(datafileBuilderBlock)
116-
117-
optimizely = (Optimizely.initWithBuilderBlock({(builder)in
118-
builder!.datafile = jsonData
119-
builder!.eventDispatcher = eventDispatcher
120-
builder!.userProfile = OPTLYUserProfile.init()
121-
builder!.datafileManager = datafileManager;
122-
}))
123-
} catch {
124-
print("invalid json data")
125-
}
126-
}
127-
128-
return optimizely!
129-
}
130-
131-
// ---- initialize Optimizely from a datafile download ----
132-
func initializeOptimizely(projectId:String, completionHandler: @escaping (_ optimizely:Optimizely?) -> Void) -> Void {
133-
134-
let networkService = OPTLYNetworkService()
135-
136-
// create the event dispatcher
137-
let eventDispatcherBuilderBlock : OPTLYEventDispatcherBuilderBlock = {[weak self] (builder)in
138-
builder?.eventDispatcherDispatchInterval = self!.eventDispatcherDispatchInterval
139-
}
140-
let eventDispatcher = OPTLYEventDispatcher.initWithBuilderBlock(eventDispatcherBuilderBlock)
141-
142-
// create the datafile manager
143-
let datafileBuilderBlock : OPTLYDatafileManagerBuilderBlock = {[weak self] (builder) in
144-
builder?.datafileFetchInterval = TimeInterval(self!.datafileManagerDownloadInterval)
145-
builder?.projectId = self!.projectId
146-
}
147-
let datafileManager = OPTLYDatafileManager.initWithBuilderBlock(datafileBuilderBlock)
148-
149-
networkService.downloadProjectConfig(projectId) { (data, response, error) in
150-
let projectConfig = OPTLYProjectConfig.initWithBuilderBlock({ (builder) in
151-
builder?.datafile = data!
152-
builder?.userProfile = OPTLYUserProfile.init()
153-
});
154-
print("projectConfig: ", projectConfig)
155-
156-
let optimizely : Optimizely? = (Optimizely.initWithBuilderBlock({(builder)in
157-
builder!.datafile = data;
158-
builder!.eventDispatcher = eventDispatcher;
159-
builder!.userProfile = OPTLYUserProfile.init()
160-
builder!.datafileManager = datafileManager
161-
}));
162-
163-
completionHandler(optimizely)
164-
}
165-
}
166-
167120
}
168121

0 commit comments

Comments
 (0)