Last active
March 9, 2023 08:46
-
-
Save ozgurshn/73d8d583fbc8916bae2abd7ad61b87ef to your computer and use it in GitHub Desktop.
Swift UI Wrapper for RevenueCat's Swift Paywall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage sample