IAP恢复

时间:2015-07-11 00:18:01

标签: iphone xcode swift in-app-purchase

我在恢复购买的产品时遇到了问题。每次用户点击“恢复按钮”时,解锁内容在检查用户是否已登录之前是否有效,是否已购买。它只是解锁。所以这是我的问题:它是怎么回事?我添加具有恢复功能的代码并购买一个。顺便说一下购买是完美的。

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

1 个答案:

答案 0 :(得分:0)

我得到了答案! 主要的问题是我没有完成交易.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 谢谢大家!