6

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
        }
    }
}
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
ced
  • 86
  • 8
  • Are you sure that you are completing the transaction correctly? Can you show your updatedTransactions code? – Paulw11 Feb 09 '18 at 02:26
  • I edited my updatedTransactions into the question. I have tried it with and without the `break` in each `case` - didn't seem to make a difference either way. – ced Feb 09 '18 at 02:34
  • It shouldn't matter but you should say `queue.finishTransaction` rather than `SKPaymentQueue.default().finishTransaction` – Paulw11 Feb 09 '18 at 03:25

0 Answers0