Restore InApp purchase using swift, iOS

青春壹個敷衍的年華 提交于 2019-12-01 00:29:13

问题


I am implementing restore in app purchase. I have a button whose action is

@IBAction func restorePurchases(send : AnyObject){

SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
// if (success) { 
// I want to do something here      
// }
}

My question is.

  1. Is this the right way to restore?
  2. How can we verify success action for restoring purchases?

回答1:


don't forget to check if you can make a payment:

if (SKPaymentQueue.canMakePayments()) {
  SKPaymentQueue.default().restoreCompletedTransactions()
}

for check if the restore was good you have to follow the protocol: SKPaymentTransactionObserver and then implement the method:

func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)

and subscribe to the event by doing:

SKPaymentQueue.default().add(self)

Finally here is an exemple of how I check the result:

func paymentQueue(_ queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)    {
  print("Received Payment Transaction Response from Apple");
  for transaction in transactions {
    switch transaction.transactionState {
    case .purchased, .restored:
      print("Purchased purchase/restored")
      SKPaymentQueue.default().finishTransaction(transaction as SKPaymentTransaction)
      break
    case .failed:
      print("Purchased Failed")
      SKPaymentQueue.default().finishTransaction(transaction as SKPaymentTransaction)
      break
    default:
      print("default")
      break
    }
  }
}


来源:https://stackoverflow.com/questions/28734890/restore-inapp-purchase-using-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!