Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Last active March 9, 2023 08:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ozgurshn/73d8d583fbc8916bae2abd7ad61b87ef to your computer and use it in GitHub Desktop.
Save ozgurshn/73d8d583fbc8916bae2abd7ad61b87ef to your computer and use it in GitHub Desktop.
Swift UI Wrapper for RevenueCat's Swift Paywall
//
// SwiftUIPaywall.swift
// PhotoText
//
// Created by Ozgur Sahin on 8/27/20.
// Copyright © 2020 com.ozgur. All rights reserved.
//
import SwiftUI
import Purchases
public struct SwiftUIPaywall: UIViewControllerRepresentable {
private let purchaseCompleted: (_ paywall: SwiftPaywall, _ transaction: SKPaymentTransaction, _ purchaserInfo: Purchases.PurchaserInfo, _ package:Purchases.Package)-> Void
private let purchaseFailed: (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error, _ userCancelled: Bool)->Void
private let purchaseRestored: (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error?)->Void
@Environment(\.presentationMode) private var presentationMode
init(purchaseCompleted: @escaping (_ paywall: SwiftPaywall, _ transaction: SKPaymentTransaction, _ purchaserInfo: Purchases.PurchaserInfo, _ package:Purchases.Package
) -> Void,
purchaseFailed: @escaping (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error, _ userCancelled: Bool)->Void,
purchaseRestored: @escaping (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error?)->Void
) {
self.purchaseCompleted = purchaseCompleted
self.purchaseFailed = purchaseFailed
self.purchaseRestored = purchaseRestored
}
public func makeUIViewController(context: Context) -> UIViewController {
//TODO: fix
let picker = SwiftPaywall(
termsOfServiceUrlString: "",
privacyPolicyUrlString: "")
picker.delegate = context.coordinator
return picker
}
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
public func makeCoordinator() -> Coordinator {
Coordinator(
purchaseCompleted: self.purchaseCompleted,
purchaseFailed: self.purchaseFailed,
purchaseRestored: self.purchaseRestored
)
}
final public class Coordinator: NSObject, SwiftPaywallDelegate {
private let _purchaseFailed: (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error, _ userCancelled: Bool)->Void
private let _purchaseRestored: (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error?)->Void
private let _purchaseCompleted: (_ paywall: SwiftPaywall, _ transaction: SKPaymentTransaction, _ purchaserInfo: Purchases.PurchaserInfo, _ package:Purchases.Package)-> Void
func purchaseCompleted(paywall: SwiftPaywall, transaction: SKPaymentTransaction, purchaserInfo: Purchases.PurchaserInfo, package: Purchases.Package) {
self._purchaseCompleted(paywall, transaction, purchaserInfo, package)
}
func purchaseFailed (paywall: SwiftPaywall, purchaserInfo: Purchases.PurchaserInfo?, error: Error, userCancelled: Bool)
{
self._purchaseFailed(paywall, purchaserInfo, error, userCancelled)
}
func purchaseRestored ( paywall: SwiftPaywall, purchaserInfo: Purchases.PurchaserInfo?, error: Error?)
{
self._purchaseRestored(paywall, purchaserInfo, error)
}
init(
purchaseCompleted: @escaping (_ paywall: SwiftPaywall, _ transaction: SKPaymentTransaction, _ purchaserInfo: Purchases.PurchaserInfo, _ package:Purchases.Package
) -> Void,
purchaseFailed: @escaping (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error, _ userCancelled: Bool)->Void,
purchaseRestored: @escaping (_ paywall: SwiftPaywall, _ purchaserInfo: Purchases.PurchaserInfo?, _ error: Error?)->Void
) {
self._purchaseCompleted = purchaseCompleted
self._purchaseFailed = purchaseFailed
self._purchaseRestored = purchaseRestored
}
}
}
@ozgurshn
Copy link
Author

Usage sample

.sheet(isPresented: self.$imageSavedAlert) {
                            SwiftUIPaywall(
                                purchaseCompleted: {paywall, transaction, purchaserInfo, package  in
                                    
                            },
                                 purchaseFailed : {paywall,  purchaserInfo ,  error,  userCancelled in
                                                                print(userCancelled)
                            },
                                 purchaseRestored: {paywall,  purchaserInfo,  error  in
                                    
                        }
                                                         )
                    }

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