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)
Purchases.shared.purchaserInfo { (purchaserInfo, error) in
self.configureCatContentFor(purchaserInfo: purchaserInfo)
}
}
2019-11-13 11:08:54 -08:00
func configureCatContentFor(purchaserInfo: Purchases.PurchaserInfo?) {
2019-01-31 12:46:25 -08:00
// set the content based on the user subscription status
if let purchaserInfo = purchaserInfo {
2019-08-20 16:57:08 -07:00
if purchaserInfo.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
if let purchaseDate = purchaserInfo.purchaseDate(forEntitlement: "pro_cat") {
self.purchaseDateLabel.text = "Purchase Date: \(dateFormatter.string(from: purchaseDate))"
}
if let expirationDate = purchaserInfo.expirationDate(forEntitlement: "pro_cat") {
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() {
Purchases.shared.restoreTransactions { (purchaserInfo, error) in
if let e = error {
print("RESTORE ERROR: - \(e.localizedDescription)")
}
self.configureCatContentFor(purchaserInfo: purchaserInfo)
}
}
}