内购恢复

IAP Restoration

我在恢复购买的产品时遇到了问题。每次用户单击“恢复”按钮时,解锁内容都会在检查用户是否登录、是否购买之前起作用。它只是解锁。所以这是我的问题:如何做对?我添加了具有恢复功能的代码并购买了一个。顺便说一句,购买非常完美。

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

我得到答案了! 主要问题是我没有在 .Restored 中完成交易,所以我在 .Purchased 和 .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
                }
           }
      }
 }

下一步 - 我没有检查客户是否可以购买。但在此之前我做了观察者:

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

所以,这很简单 :D 谢谢大家!