Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: IAP updatedTransactions not called on .Purchased

I have a problem with my code. The function updatedTransactions is only called once while the transaction is .Purchasing and is not called after the transaction has ben completed.

func buyProduct(product: SKProduct) {
  let payment = SKPayment(product: product)
  SKPaymentQueue.defaultQueue().addTransactionObserver(self)
  SKPaymentQueue.defaultQueue().addPayment(payment)
}

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

  for transaction in transactions {
    print(transaction)
    switch (transaction.transactionState) {
      case .Purchased, .Restored:
        print("Completed")
        complete(transaction)
        break
      case .Failed:
        fail(transaction)
        break
      default:
        break
    }
  }
}
like image 690
Hugo Hammarstrom Avatar asked Nov 28 '16 22:11

Hugo Hammarstrom


1 Answers

Sorry I might answer a bit late, but it might be related to this question. Are you testing your IAP from Xcode when it doesn't reach the .purchased state?

If yes, then you need to set your device's App Store ID to one you have created on iTunes Connect (User menu, Sandbox testers).

If no, then maybe it could be because your SKPaymentQueue already contains too many transactions waiting to be finished. You can check it with SKPaymentQueue.default().transactions.

In my case I had 28 transactions waiting to be finished because I had a bad switch case statement during the purchase phase.

If this is the case, you can add those lines in your viewDidLoad to finish each one of them (don't forget to remove them after you find why they didn't finish in your code):

for transaction: AnyObject in SKPaymentQueue.default().transactions {
    SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
}
like image 189
Tulleb Avatar answered Sep 30 '22 21:09

Tulleb