CampaignViewController.swift
3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// CampaignViewController.swift
// SwiftWarplyFramework
//
// Created by Βασιλης Σκουρας on 11/5/22.
//
import Foundation
import UIKit
import WebKit
import SwiftEventBus
@objc public class CampaignViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
@IBOutlet weak var webview: WKWebView!
public var campaignUrl: String = ""
public override func viewDidLoad() {
super.viewDidLoad()
print("Webview url: " + campaignUrl)
self.hidesBottomBarWhenPushed = true
setBackButton()
webview.navigationDelegate = self
if let url = URL(string: campaignUrl) {
webview.load(URLRequest(url: url))
webview.allowsBackForwardNavigationGestures = true
}
webview.configuration.userContentController.add(self, name: "Cosmote")
}
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let event = message.body as? String {
let eventArray = event.split(separator: ":")
if (eventArray[0] == "event") {
if (eventArray[1] == "closeArtwork") {
self.navigationController?.popViewController(animated: true)
} else if (eventArray[1] == "addUserTag") {
let questionnaireEvent = swiftApi.QuestionnaireEventModel()
questionnaireEvent._name = String(eventArray[1])
questionnaireEvent._parameter = String(eventArray[2])
SwiftEventBus.post("questionnaire", sender: questionnaireEvent)
swiftApi().setUserTag(String(eventArray[2]))
self.navigationController?.popViewController(animated: true)
} else if (eventArray[1] == "couponRetrieved") {
let couponEvent = swiftApi.CouponEventModel()
SwiftEventBus.post("coupon_retrieved", sender: couponEvent)
} else if (eventArray[2] == "serviceDisabled") {
let pacingService = swiftApi.WarplyPacingCardServiceEnabledModel()
pacingService._isEnabled = false
SwiftEventBus.post("pacing_service", sender: pacingService)
} else if (eventArray[2] == "serviceEnabled") {
let pacingService = swiftApi.WarplyPacingCardServiceEnabledModel()
pacingService._isEnabled = true
SwiftEventBus.post("pacing_service", sender: pacingService)
} else if (eventArray[2] == "shortcutEnabled") {
let pacingEvent = swiftApi.WarplyPacingCardEventModel()
pacingEvent._isVisible = true
SwiftEventBus.post("pacing_visible", sender: pacingEvent)
} else if (eventArray[2] == "shortcutDisabled") {
let pacingEvent = swiftApi.WarplyPacingCardEventModel()
pacingEvent._isVisible = false
SwiftEventBus.post("pacing_visible", sender: pacingEvent)
} else if (eventArray[1] == "request" || eventArray[1] == "response") {
print("**************** WARPLY Webview Log START *****************")
print(eventArray[2])
print("**************** WARPLY Webview Log END *****************")
}
}
}
}
}