简体   繁体   中英

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
    }
  }
}

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM