如何正确还原应用内购买? [英] How to restore in-app purchases correctly?

查看:146
本文介绍了如何正确还原应用内购买?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些应用内购买.但是,我读到我需要提供一种还原它们的方法,以防它们意外删除我的应用程序.它们是非消耗性物品,例如皮肤和纹理.我正在阅读此站点上的教程: https://code.tutsplus.com/tutorials/in-app-purchase-tutorial-with-swift-3-ios-sdk--cms-27595

I have a few in-app purchases. However, I read I need to provide a way to restore them in case that they delete my application by accident. They are nonconsumable items, like skins and textures. I was reading a tutorial on this site: https://code.tutsplus.com/tutorials/in-app-purchase-tutorial-with-swift-3-ios-sdk--cms-27595

但是,他们对恢复交易的解释使我感到困惑.他们这样做:

However, their explanation of restoring transactions confused me. They did this:

@IBAction func restorePurchaseButt(_ sender: Any) {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
nonConsumablePurchaseMade = true
UserDefaults.standard.set(nonConsumablePurchaseMade, forKey: "nonConsumablePurchaseMade")

UIAlertView(title: "IAP Tutorial",
message: "You've successfully restored your purchase!",
delegate: nil, cancelButtonTitle: "OK").show()
}

但是,他们从未向我展示他们实际在哪里使用了paymentQueueRestoreCompletedTransactionsFinished().我了解他们正在使用restorePurchaseButt()还原购买的商品.但是,我对这两种方法都不了解.

However, they never showed me where they actually used the paymentQueueRestoreCompletedTransactionsFinished(). I understand they are using the restorePurchaseButt() to restore the purchases. However, I don't understand both of these methods.

SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()

该代码段如何检查用户是否进行了购买?我看不到任何if语句.而且我认为也许restoreCompletedTransactions()会触发paymentQueueRestoreCompletedTransactionsFinished(),但我不确定吗?但是,即使这样做, paymentQueueRestoreCompletedTransactionsFinished()如何检查玩家是否进行了这些交易?如果有多个应用,现在如何还原应用内购买?

How does that snippet of code even check if the user made the purchase? I don't see any if statements. And I assumed maybe restoreCompletedTransactions() triggers paymentQueueRestoreCompletedTransactionsFinished(), but I am not sure? However, even if it does, how does paymentQueueRestoreCompletedTransactionsFinished() check if the player made those transactions or not? How does it now what in-app purchases to restore when there are multiple?

推荐答案

就像在还原按钮中所做的那样,您需要添加:

As you did in the button to restore, you need to add:

SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()

然后,您还需要为iTunesConnect中的不同应用程序内购买添加此功能.将案例的"ProductID"更改为来自iTunesConnect的产品ID,并添加与您在应用内购买一样多的案例.

Then you also need to add this function with the different in-app purchases you have in iTunesConnect. Change "ProductID" for case to be a product ID from iTunesConnect and add as many cases as you have in-app purchases.

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
    for transaction in queue.transactions {
        let t: SKPaymentTransaction = transaction
        let prodID = t.payment.productIdentifier as String

        switch prodID {
        case "ProductID1":
            // implement the given in-app purchase as if it were bought
        case "ProductID2":
            // implement the given in-app purchase as if it were bought
        default:
            print("iap not found")
        }
    }
}

要回答您的问题:看来您正在关注的这个特定教程专注于教会您应用内购买的总体思路,并使用其示例购买而不是可用于多种产品的代码.

To answer your questions: It looks like this particular tutorial you were following was focusing on teaching you the overarching ideas of in-app purchases and used their example purchase rather than code that can be used for multiple products.

Apple检查该项目是否以前通过按钮时调用的restoreCompletedTransactions()函数购买.这是Apple提供的StoreKit框架的一部分.一旦检查,它将自动触发paymentQueueRestoreCompletedTransactionsFinished()函数.使用上面提供的代码,它会根据发现的商品或在未找到要还原的产品时打印到控制台的方式执行不同的代码.

Apple checks if the item was previously purchased through the restoreCompletedTransactions() function that you call when the button. This is part of the StoreKit framework that Apple provides. It automatically triggers the paymentQueueRestoreCompletedTransactionsFinished() function once it has checked. With the code I provide above, it performs different code depending on which purchases it found or prints to the console if it did not find the product to restore.

这篇关于如何正确还原应用内购买?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆