The use case here is that the user has never purchased my in-app purchase but taps the Restore button. I want to let the user know that the restore has failed because there is nothing to restore.
The problem is that I'm not getting any event. So I don't know that anything has happened.
When my user taps the Restore button in my interface, I call
SKPaymentQueue.default().restoreCompletedTransactions
However, after that nothing is happening. I expect my SKPaymentTransactionObserver paymentQueue(_:updatedTransactions:)
to be called, but it isn't.
I tried implementing paymentQueue(_:restoreCompletedTransactionsFailedWithError:)
, but it isn't being called either.
You're so close! You need to implement this SKPaymentTransactionObserver method:
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
// ...
}
That method is called after restoreCompletedTransactions
, if the device was able to communicate with the App Store — regardless of whether the restore succeeded because the user did indeed have purchases to restore, or failed because the user had no purchases to restore.
Unfortunately, you don't get any information here about what just happened. I regard that as a flaw in the StoreKit architecture.
However, if the restore succeeded, we will have passed through paymentQueue(_:updatedTransactions:)
with a transactionState
of .restored
. So presumably you are setting some sort of UserDefaults Bool (or similar) to indicate that the user has made this purchase.
Well, that happens or doesn't happen before paymentQueueRestoreCompletedTransactionsFinished
is called. So in paymentQueueRestoreCompletedTransactionsFinished
you can look to see whether that UserDefaults Bool is set, and if it isn't, you can guess that the user probably never made any purchase in the first place, and respond accordingly.
It's guesswork, but it's better than nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With