我已经完成了一个小应用程序,我有一个非消耗品购买选项。它在App Store上。
购买该产品运行正常。这是我的恢复购买功能似乎什么都不做。
我已为恢复购买@IBAction
添加了此代码:
@IBAction func restorePurchases(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
但是当我点击恢复购买按钮时没有任何反应。
我想我必须添加一个检查恢复是否成功的函数。我打算将代码修改为以下内容:
@IBAction func restorePurchases(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Restored:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
var alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
break;
case .Failed:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
var alert = UIAlertView(title: "Sorry", message: "Your purchase(s) could not be restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
break;
default:
break;
}
}
}
这会起作用吗?
我已经完成了与影响恢复购买交易相关的每个主题,我的研究使我了解了上述内容。因此,我不认为这是一个问题的副本,但也许可以澄清如何成功恢复面临类似情况的其他人的购买。
答案 0 :(得分:2)
你的代码在大多数情况下看起来都很好,尽管有些部分似乎来自较旧的教程。您应该进行一些更改,其中之一是您需要再次调用unlockProduct函数。
这是我使用的代码(Swift 3)。
/// Updated transactions
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
// Transaction is being added to the server queue.
case .purchased:
// Transaction is in queue, user has been charged. Client should complete the transaction.
defer {
queue.finishTransaction(transaction)
}
let productIdentifier = transaction.payment.productIdentifier
unlockProduct(withIdentifier: productIdentifier)
case .failed:
// Transaction was cancelled or failed before being added to the server queue.
defer {
queue.finishTransaction(transaction)
}
let errorCode = (transaction.error as? SKError)?.code
if errorCode == .paymentCancelled {
print("Transaction failed - user cancelled payment")
} else if errorCode == .paymentNotAllowed { // Will show alert automatically
print("Transaction failed - payments are not allowed")
} else {
print("Transaction failed - other error")
// Show alert with localised error description
}
case .restored:
// Transaction was restored from user's purchase history. Client should complete the transaction.
defer {
queue.finishTransaction(transaction)
}
if let productIdentifier = transaction.original?.payment.productIdentifier {
unlockProduct(withIdentifier: productIdentifier)
}
case .deferred:
// The transaction is in the queue, but its final status is pending external action
// e.g family member approval (FamilySharing).
// DO NOT freeze up app. Treate as if transaction has not started yet.
}
}
}
使用委托方法显示恢复警报
/// Restore finished
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
guard queue.transactions.count != 0 else {
// showAlert that nothing restored
return
}
// show restore successful alert
}
/// Restore failed
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError) {
/// handle the restore error if you need to.
}
解锁产品只是一种方法我相信你已经拥有了。
func unlockProduct(withIdentifier productIdentifier: String) {
switch productIdentifier {
/// unlock product for correct ID
}
}
作为旁注,你应该移动这一行
SKPaymentQueue.default().add(self)
退出恢复购买功能并将其放入viewDidLoad。
Apple建议您在应用启动后立即添加事务观察器,并仅在应用关闭时将其删除。遗憾的是,很多教程都没有正确地教你这个。这样您就不能确定任何未完成的事务,例如由于网络错误,将始终正确恢复。
https://developer.apple.com/library/content/technotes/tn2387/_index.html
在我的实际项目中,我的IAP代码在Singleton类中,所以我实际上使用委托将unlockProduct方法转发到处理gameData的类。我还可以确保在应用启动时添加观察者。
希望这有帮助