forked from RevenueCat/purchases-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserViewController.swift
123 lines (99 loc) · 4.63 KB
/
UserViewController.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
//
// UserViewController.swift
// Magic Weather
//
// Created by Cody Kerns on 12/14/20.
//
import UIKit
import RevenueCat
/*
View controller to display user's details like subscription status and ID's.
Configured in /Resources/UI/Main.storyboard
*/
class UserViewController: UIViewController {
@IBOutlet var userIdLabel: UILabel!
@IBOutlet var statusLabel: UILabel!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
/// - Refresh details when the User tab is viewed
refreshUserDetails()
}
func refreshUserDetails() {
self.userIdLabel.text = Purchases.shared.appUserID
if Purchases.shared.isAnonymous {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log In", style: .done, target: self, action: #selector(presentLoginDialog))
} else {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log Out", style: .done, target: self, action: #selector(logout))
}
Purchases.shared.getCustomerInfo { (purchaserInfo, error) in
if purchaserInfo?.entitlements[Constants.entitlementID]?.isActive == true {
self.statusLabel.text = "Active"
self.statusLabel.textColor = .green
} else {
self.statusLabel.text = "Not Active"
self.statusLabel.textColor = .red
}
}
}
}
/*
How to login and identify your users with the Purchases SDK.
These functions mimic displaying a login dialog, identifying the user, then logging out later.
Read more about Identifying Users here: https://docs.revenuecat.com/docs/user-ids
*/
extension UserViewController {
/// - Login method. Replace this with your own login sequence.
@objc
func presentLoginDialog() {
let alert = UIAlertController(title: "Log In", message: "Enter your username.", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "Username"
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Log In", style: .default, handler: { (action) in
/// - Ensure a username was entered
if let username = alert.textFields?.first?.text, username.isEmpty == false {
#warning("Public-facing usernames aren't optimal for user ID's - you should use something non-guessable, like a non-public database ID. For more information, visit https://docs.revenuecat.com/docs/user-ids.")
/// - Call `identify` with the Purchases SDK with the unique user ID
Purchases.shared.logIn(username) { (purchaserInfo, created, error) in
if let error = error {
self.present(UIAlertController.errorAlert(message: error.localizedDescription), animated: true, completion: nil)
}
self.refreshUserDetails()
}
}
}))
self.present(alert, animated: true, completion: nil)
}
/// - Log out method
@objc
func logout() {
/*
The current user ID is no longer valid for your instance of *Purchases* since the user is logging out, and is no longer authorized to access purchaserInfo for that user ID.
`reset` clears the cache and regenerates a new anonymous user ID.
Note: Each time you call `reset`, a new installation will be logged in the RevenueCat dashboard as that metric tracks unique user ID's that are in-use. Since this method generates a new anonymous ID, it counts as a new user ID in-use.
*/
Purchases.shared.logOut { (purchaserInfo, error) in
if let error = error {
self.present(UIAlertController.errorAlert(message: error.localizedDescription), animated: true, completion: nil)
} else {
self.refreshUserDetails()
}
}
}
}
/*
How to restore purchases using the Purchases SDK. Read more about restoring purchases here: https://docs.revenuecat.com/docs/making-purchases#restoring-purchases
*/
extension UserViewController {
/// - Restore purchases method
@IBAction
func restorePurchases() {
Purchases.shared.restorePurchases { (purchaserInfo, error) in
if let error = error {
self.present(UIAlertController.errorAlert(message: error.localizedDescription), animated: true, completion: nil)
}
self.refreshUserDetails()
}
}
}