1// In ViewController.swift
2
3override func viewWillAppear(_ animated: Bool) {
4 super.viewWillAppear(animated)
5
6 // Note self is captured weakly
7 let handler = { [weak self] (campaign: EVGCampaign) -> Void in
8 // Safest to perform a single method/operation on weakSelf, which will simply be a no-op if weakSelf is nil:
9 self?.handleCampaign(campaign: campaign)
10 }
11
12 // The target string uniquely identifies the expected data schema - here, a featured product:
13 evergageScreen?.setCampaignHandler(handler, forTarget: "Featured Product")
14}
15
16func handleCampaign(campaign: EVGCampaign) {
17 // Validate the campaign data since it's dynamic JSON. Avoid processing if fails.
18 guard let featuredProductName = campaign.data["featuredProductName"] as? String
19 else { return }
20 if (featuredProductName.isEmpty) { return }
21
22 // Check if the same content is already visible/active (see Usage Details above).
23 if (activeCampaign && activeCampaign.equals(campaign)) {
24 NSLog("Ignoring campaign name %@ since equivalent content is already active",
25 campaign.campaignName)
26 return
27 }
28
29 // Track the impression for statistics even if the user is in the control group.
30 evergageScreen?.trackImpression(campaign)
31
32 // Only display the campaign if the user is not in the control group.
33 if (!campaign.isControlGroup) {
34 // Keep active campaign as long as needed for (re)display and comparison
35 activeCampaign = campaign
36 NSLog("New active campaign name %@ for target %@ with data %@",
37 campaign.campaignName, campaign.target, campaign.data)
38
39 featuredProductLabel.text = String(format: "Our featured product is %@!", featuredProductName)
40 }
41}