当非购买用户按下iOS中的恢复购买按钮时会发生什么?

时间:2015-04-21 07:16:17

标签: ios swift in-app-purchase

我的沙箱测试帐户可以购买非消耗品并将其恢复。一切正常。但是,如果帐户之前没有购买过该项目,则按恢复按钮不会执行任何操作。我在调试面板中看不到任何内容。我希望iOS能够检测某个用户是否购买了该商品,如果没有,则会显示一条消息,要求他们购买该商品。它是否像那样工作或当前的行为完全可以接受?

以下是连接到主故事板内按钮的恢复购买代码(Swift):

@IBAction func restoreButtonPressed(sender: UIButton) {
    statusLabel.text = "Status: Restoring Purchase"
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

其他实施的方法包括:

适用于正常购买

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

适用于正常恢复

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {}

之前永远不会看到来自此方法的消息
func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!) {}

谢谢!

2 个答案:

答案 0 :(得分:4)

您可以检查队列是否有任何已退回的交易,如果不是,则表示没有要恢复的购买:

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
  if queue.transactions.count == 0 {
    let alert = UIAlertView()
    alert.title = "Oops"
    alert.message = "There are no purchases to restore, please buy one"
    alert.addButtonWithTitle("Buy")
    alert.addButtonWithTitle("Cancel")
    alert.show()
 }
}

答案 1 :(得分:0)

您可以尝试以下方法:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {


    for transaction in transactions {

        switch transaction.transactionState {
        case .purchasing: print("Payment is being processed")

        case .purchased: print("USER PURCHASED YOUR PRODUCT")

            SKPaymentQueue.default().finishTransaction(transaction)

        case .restored:

            if transaction.original?.transactionIdentifier != nil { // this identifier uniquely identifies a completed tansaction.
        //    queue.restoreCompletedTransactions()

                print("THE BUYER BOUGHT THIS PRODUCT BEFORE")

            } else {
             /*
              display UIAlertController showing an error
              there is no receipts, user never purchased this product.
                                */
            }


        case .failed:
            self.dismiss(animated: true, completion: nil)

            SKPaymentQueue.default().finishTransaction(transaction)
        case .deferred:

           print("pending")

        @unknown default:
           break
        }


    }


}