Files
purchases-ios/Examples/LegacySwiftExample/SwiftExample/CatsViewController.swift
T

76 lines
2.7 KiB
Swift
Raw Normal View History

2019-01-31 12:46:25 -08:00
//
// CatsViewController.swift
// SwiftExample
//
// Created by Ryan Kotzebue on 1/9/19.
// Copyright © 2019 RevenueCat. All rights reserved.
//
import UIKit
2021-09-03 14:17:37 -03:00
import RevenueCat
2019-01-31 12:46:25 -08:00
class CatsViewController: UIViewController {
@IBOutlet weak var goPremiumButton: UIButton!
@IBOutlet weak var restorePurchasesButton: UIButton!
@IBOutlet weak var catContentLabel: UILabel!
@IBOutlet weak var expirationDateLabel: UILabel!
@IBOutlet weak var purchaseDateLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
goPremiumButton.addTarget(self, action: #selector(goPremiumButtonTapped), for: .touchUpInside)
restorePurchasesButton.addTarget(self, action: #selector(restorePurchasesButtonTapped), for: .touchUpInside)
2021-10-22 15:59:13 -07:00
Purchases.shared.getCustomerInfo { (customerInfo, error) in
self.configureCatContentFor(customerInfo: customerInfo)
2019-01-31 12:46:25 -08:00
}
}
2021-10-22 15:59:13 -07:00
func configureCatContentFor(customerInfo: CustomerInfo?) {
2019-01-31 12:46:25 -08:00
// set the content based on the user subscription status
2021-10-22 15:59:13 -07:00
if let customerInfo = customerInfo {
2019-01-31 12:46:25 -08:00
2021-10-22 15:59:13 -07:00
if customerInfo.entitlements["pro_cat"]?.isActive == true {
2019-01-31 12:46:25 -08:00
print("Hey there premium, you're a happy cat 😻")
self.catContentLabel.text = "😻"
self.goPremiumButton.isHidden = true
self.restorePurchasesButton.isHidden = true
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
2021-10-22 15:59:13 -07:00
if let purchaseDate = customerInfo.purchaseDate(forEntitlement: "pro_cat") {
2019-01-31 12:46:25 -08:00
self.purchaseDateLabel.text = "Purchase Date: \(dateFormatter.string(from: purchaseDate))"
}
2021-10-22 15:59:13 -07:00
if let expirationDate = customerInfo.expirationDate(forEntitlement: "pro_cat") {
2019-01-31 12:46:25 -08:00
self.expirationDateLabel.text = "Expiration Date: \(dateFormatter.string(from: expirationDate))"
}
} else {
print("Happy cats are only for premium members 😿")
self.catContentLabel.text = "😿"
}
}
}
@objc func goPremiumButtonTapped() {
navigationController?.popViewController(animated: true)
}
@objc func restorePurchasesButtonTapped() {
2021-10-22 15:59:13 -07:00
Purchases.shared.restoreTransactions { (customerInfo, error) in
2019-01-31 12:46:25 -08:00
if let e = error {
print("RESTORE ERROR: - \(e.localizedDescription)")
}
2021-10-22 15:59:13 -07:00
self.configureCatContentFor(customerInfo: customerInfo)
2019-01-31 12:46:25 -08:00
}
}
}