问题
I am having trouble restoring an IAP in Swift 4/iOS 11. My AppDelegate
implements SKPaymentTransactionObserver
. In AppDelegate
's didFinishLaunchingWithOptions
I'm calling SKPaymentQueue.default().add(self)
. I implement the paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
function in AppDelegate as well.
From a ViewController
I can make a purchase like so:
let payment = SKPayment(product: products.first!)
SKPaymentQueue.default().add(payment)
The purchase finishes successfully - I get an updatedTransaction
with state .purchased
in my paymentQueue
observer.
However, when I call the following from this same ViewController
in order to restore the purchase:
SKPaymentQueue.default().restoreCompletedTransactions()
the paymentQueue updatedTransactions
function is never called.
It seems like my sandbox user, etc. is set up correctly since the initial purchase is working. Do sandbox users have the ability to restore purchases? Do I need to do any additional work besides calling restoreCompletedTransactions()
to restore purchases?
Update, here is my paymentQueue updatedTransactions
code that lives in AppDelegate
:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions{
switch transaction.transactionState{
case .purchased:
debugPrint("purchased it!")
menuDelegate?.unlockMenu()
//unlock the content, then
SKPaymentQueue.default().finishTransaction(transaction)
break
case .failed:
debugPrint("failed")
SKPaymentQueue.default().finishTransaction(transaction)
break
case .restored:
debugPrint("restored")
menuDelegate?.unlockMenu()
//unlock the content, then
SKPaymentQueue.default().finishTransaction(transaction)
break
case .purchasing:
debugPrint("purchasing...")
break
case .deferred:
debugPrint("deferred...")
break
}
}
}
来源:https://stackoverflow.com/questions/48697258/restorecompletedtransactions-not-calling-paymentqueue-updatedtransactions