简体   繁体   中英

IAP Restoration

I got a problem working on with restoration of purchased product. Every time user clicks on Restore Button the unlock content works before checking if user is logged in, had he purchased it or not. It just unlocks. So here is my question: How do it right? I Add the code with restoration function and purchase one. Btw purchasing works perfect.

 func restorePurchases(){
      println("hello")
      SKPaymentQueue.defaultQueue().addTransactionObserver(self)
      SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
 }

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

 func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!) {
      showAlert("error", message: "hoho")
 }

 func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
      for transaction:AnyObject in transactions {
           if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
                switch trans.transactionState{
                case .Purchased:
                     self.removeAds()
                     SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                     break
                case .Failed:
                     showAlert("Error", message: "Transaction problem")
                     SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                     break
                case .Restored:
                     self.removeAds()
                     break
                default:
                     break
                }
           }
      }
 }

I got the answer! The main problem was that I didnt finish transaction in .Restored, so i got the same thing for .Purchased and .Restored.

     func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
      for transaction:AnyObject in transactions {
           if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
                switch trans.transactionState{
                case .Purchased, .Restored:
                     self.removeAds()
                     SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                     break
                case .Failed:
                     showAlert("Error", message: "Transaction problem")
                     SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                     break
                default:
                     break
                }
           }
      }
 }

Next - i didnt check can customer make purchases or not. But before this i made the observer:

     func restorePurchases(){
      SKPaymentQueue.defaultQueue().addTransactionObserver(self)
      if SKPaymentQueue.canMakePayments(){
           SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
      }
 }

So, that was easy :D Thanks everyone!

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