-
Notifications
You must be signed in to change notification settings - Fork 22
/
TrackEventViewController.swift
125 lines (115 loc) · 4.31 KB
/
TrackEventViewController.swift
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// TrackEventViewController.swift
// SuperwallUIKitExample
//
// Created by Yusuf Tör on 05/04/2022.
//
// swiftlint:disable force_cast
import UIKit
import SuperwallKit
import Combine
final class TrackEventViewController: UIViewController {
@IBOutlet private var subscriptionLabel: UILabel!
private var subscribedCancellable: AnyCancellable?
private var cancellable: AnyCancellable?
static func fromStoryboard() -> TrackEventViewController {
let storyboard = UIStoryboard(
name: "Main",
bundle: nil
)
let controller = storyboard.instantiateViewController(
withIdentifier: "TrackEventViewController"
) as! TrackEventViewController
return controller
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
subscribedCancellable = Superwall.shared.$subscriptionStatus
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
switch status {
case .unknown:
self?.subscriptionLabel.text = "Loading subscription status."
case .active:
self?.subscriptionLabel.text = "You currently have an active subscription. Therefore, the paywall will never show. For the purposes of this app, delete and reinstall the app to clear subscriptions."
case .inactive:
self?.subscriptionLabel.text = "You do not have an active subscription so the paywall will show when clicking the button."
}
}
navigationItem.hidesBackButton = true
}
@IBAction private func logOut() {
UserDefaults.standard.setValue(false, forKey: "IsLoggedIn")
SuperwallService.reset()
self.navigationController?.popToRootViewController(animated: true)
}
@IBAction private func trackEvent() {
Superwall.shared.track(event: "campaign_trigger") { paywallState in
switch paywallState {
case .presented(let paywallInfo):
print("paywall info is", paywallInfo)
case .dismissed(let result):
switch result.state {
case .purchased(let productId):
print("The purchased product ID is", productId)
case .closed:
print("The paywall was closed.")
case .restored:
print("The product was restored.")
}
case .skipped(let reason):
switch reason {
case .holdout(let experiment):
print("The user is in a holdout group, with id \(experiment.id) and group id \(experiment.groupId)")
case .noRuleMatch:
print("The user did not match any rules")
case .eventNotFound:
print("The event wasn't found in a campaign on the dashboard.")
case .userIsSubscribed:
print("The user is subscribed.")
case .error(let error):
print("Failed to present paywall. Consider a native paywall fallback", error)
}
}
}
}
// The below function gives an example of how to track an event using Combine publishers:
/*
func trackEventUsingCombine() {
cancellable = Superwall.shared
.publisher(forEvent: "MyEvent")
.sink { paywallState in
switch paywallState {
case .presented(let paywallInfo):
print("paywall info is", paywallInfo)
case .dismissed(let result):
switch result.state {
case .closed:
print("User dismissed the paywall.")
case .purchased(productId: let productId):
print("Purchased a product with id \(productId), then dismissed.")
case .restored:
print("Restored purchases, then dismissed.")
}
case .skipped(let reason):
switch reason {
case .noRuleMatch:
print("The user did not match any rules")
case .holdout(let experiment):
print("The user is in a holdout group, with experiment id: \(experiment.id), group id: \(experiment.groupId), paywall id: \(experiment.variant.paywallId ?? "")")
case .eventNotFound:
print("The event wasn't found in a campaign on the dashboard.")
case .userIsSubscribed:
print("The user is subscribed.")
case .error(let error):
print("Failed to present paywall. Consider a native paywall fallback", error)
}
}
}
}
*/
}