Skip to content

Instantly share code, notes, and snippets.

@Yihwan
Last active December 5, 2020 13:25
Show Gist options
  • Save Yihwan/6e9e6226aaae286b0366ba4121bee8bb to your computer and use it in GitHub Desktop.
Save Yihwan/6e9e6226aaae286b0366ba4121bee8bb to your computer and use it in GitHub Desktop.
Sample usage of RevenueCat API — (NOT a perfect example)
//
// ProPurchaseManager.swift
// weight-calc
//
// Created by Yihwan Kim on 3/15/20.
// Copyright © 2020 Yihwan Kim. All rights reserved.
//
/*
MIT License
Copyright (c) 2020 Yihwan Kim
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
NOTE: This file is NOT intended to be a good illustration of how to use RevenueCat.
It's just *one* way to use RevenueCat, and maybe a bad one at that. ;)
Feedback welcome!
*/
import SwiftUI
import Purchases
class ProPurchaseManager: ObservableObject {
@Published var isProSheetPresented = false
@Published var isProSectionAlertPresented = false
var proSectionAlertTitle = ""
var proSectionAlertMessage = ""
static var package: Purchases.Package?
var hasActiveMembership: Bool {
var isActive = false
Purchases.shared.purchaserInfo { (purchaserInfo, error) in
if purchaserInfo?.entitlements["Powerlift Pro"]?.isActive == true {
isActive = true
}
}
return isActive
}
var memberSince: String {
var memberSince = ""
Purchases.shared.purchaserInfo { (purchaserInfo, error) in
let formatter = DateFormatter()
formatter.dateStyle = .medium
if let memberSinceDate = purchaserInfo?.entitlements["Powerlift Pro"]?.originalPurchaseDate {
memberSince = formatter.string(from: memberSinceDate)
}
}
return memberSince
}
func presentProSheet() -> Void {
if ProPurchaseManager.package != nil {
Haptics.light()
self.isProSheetPresented = true
return
}
Purchases.shared.offerings { (offerings, error) in
if let packages = offerings?.current?.availablePackages {
ProPurchaseManager.package = packages.first
if ProPurchaseManager.package != nil {
Haptics.light()
self.isProSheetPresented = true
}
}
}
}
func purchase() -> Void {
if ProPurchaseManager.package == nil {
return
}
Purchases.shared.purchasePackage(ProPurchaseManager.package!) { (transaction, purchaserInfo, error, userCancelled) in
if purchaserInfo?.entitlements.active.first != nil {
Haptics.success()
self.isProSheetPresented = false
}
}
}
func restorePurchase() -> Void {
Purchases.shared.restoreTransactions { (purchaserInfo, error) in
if let error = error {
Haptics.error()
self.proSectionAlertTitle = "Error"
// TODO: Shouldn't hard-code "Internet connection" error. Also couldn't figure out how to access
// the more descriptive error (I think it was in NSError?).
self.proSectionAlertMessage = "\(error.localizedDescription) Please ensure you are connected to the Internet and try again later."
}
if let purchaserInfo = purchaserInfo {
Haptics.success()
let formatter = DateFormatter()
var memberSinceLocal = ""
formatter.dateStyle = .medium
if let memberSinceDate = purchaserInfo.entitlements["Powerlift Pro"]?.originalPurchaseDate {
memberSinceLocal = formatter.string(from: memberSinceDate)
}
// TODO: This shows up even if user has not purchased Pro.
self.proSectionAlertTitle = "Purchase Restored"
self.proSectionAlertMessage = "Thank you for being a Powerlift Pro member\(memberSinceLocal.isEmpty ? "" : " since \(memberSinceLocal)")"
}
self.isProSectionAlertPresented = true
}
}
}
@jvbnl
Copy link

jvbnl commented Dec 5, 2020

Thanks for this clear example! I'm struggling a lot with implementing this in a view. Do you have any example code you'd like to share? Like how can I show/hide content based on entitlements AND how can users subscribe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment