0

I am following the tutorial for IAP from the following site: https://betterprogramming.pub/set-up-your-swiftui-app-to-support-in-app-purchases-ef2e0a11d10c

Here is the portion of the code that handles the transaction and the states:

extension IAPManager: SKPaymentTransactionObserver {
  
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
 
        transactions.forEach { (transaction) in
  
            switch transaction.transactionState {
  
            case .purchased:
                SKPaymentQueue.default().finishTransaction(transaction)
                purchasePublisher.send(("Purchased ",true))
  
            case .restored:
                totalRestoredPurchases += 1
                SKPaymentQueue.default().finishTransaction(transaction)
                purchasePublisher.send(("Restored ",true))
   
            case .failed:
                if let error = transaction.error as? SKError {
                    purchasePublisher.send(("Payment Error \(error.code) ",false))
                    print("Payment Failed \(error.code)")
                }
                SKPaymentQueue.default().finishTransaction(transaction)
  
            case .deferred:
                print("Ask Mom ...")
                purchasePublisher.send(("Payment Diferred ",false))
  
            case .purchasing:
                print("working on it...")
                purchasePublisher.send(("Payment in Process ",false))
  
            default:
                break
  
            }

        }

    }   

}

In short, it checks each case on the queue for each buy request you placed with the server and sends the status back to the SwiftUI interface through the purchasePublisher PassThruSubject that looks like this:

let purchasePublisher = PassthroughSubject<(String, Bool), Never>()

That is the part I am confused about!? How do I access the purchasePublisher so that I can check on the SwiftUI view (SwiftUI interface) that the purchase was in fact completed successfully so that then I can take action accordingly?

trndjc
  • 11,654
  • 3
  • 38
  • 51
Learn2Code
  • 1,974
  • 5
  • 24
  • 46

1 Answers1

0

purchasePublisher is an instance of PassthroughSubject, which is a publisher that broadcasts information to subscribers. You must go through this instance to access those subscriptions and you can do that with the sink method:

purchasePublisher.sink(receiveValue: { (value) in
    print(value)
})

And there are, as always, caveats. You may find something like this useful for what you're doing: .send() and .sink() do not seem to work anymore for PassthroughSubject in Xcode 11 Beta 5

trndjc
  • 11,654
  • 3
  • 38
  • 51