InAppPurchaseManager - Stupid Simple implementation
Stupid Simple ready to use
import StoreKit
class InAppPurchaseManager: NSObject, SKPaymentTransactionObserver {
static let shared = InAppPurchaseManager()
private override init() {}
func startObservingTransactions() {
SKPaymentQueue.default().add(self)
}
func stopObservingTransactions() {
SKPaymentQueue.default().remove(self)
}
func purchase(product: SKProduct) {
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment)
}
func restorePurchases() {
SKPaymentQueue.default().restoreCompletedTransactions()
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
// The purchase was successful.
break
case .failed:
// The purchase failed.
break
case .restored:
// The purchase was restored.
break
default:
break
}
}
}
}
To use this class, you can call the startObservingTransactions()
method when your app launches to begin observing payment transactions. You can then call the purchase(product:)
method to initiate a purchase, and the restorePurchases()
method to allow the user to restore their purchases. The paymentQueue(_:updatedTransactions:)
method will be called whenever there is a change in the payment queue, allowing you to track the progress of the transaction and respond to changes.