Restore InApp purchase using swift, iOS

前端 未结 1 1341
情书的邮戳
情书的邮戳 2021-01-17 15:53

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

@IBAction func restorePurchases(send : AnyObject){

SKPaymentQueue.defaultQueue()         


        
相关标签:
1条回答
  • 2021-01-17 16:17

    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
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题