Kat*_*ins 2 restore in-app-purchase ios swift
我已经完成了一个小应用程序,我有一个非消耗品的购买选项。它在 App Store 上。
购买的产品运行正常。我的恢复购买功能似乎什么也没做。
我已为恢复购买添加了此代码@IBAction:
@IBAction func restorePurchases(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
Run Code Online (Sandbox Code Playgroud)
但是当我点击恢复购买按钮时没有任何反应。
我想我必须添加一个函数来检查恢复是否成功。我计划将代码修改为以下内容:
@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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会奏效吗?
我已经完成了与影响恢复购买交易相关的每一个线程,我的研究使我达到了上述目的。所以我不认为这是一个问题的重复,但也许可以澄清如何为面临类似情况的其他人成功恢复购买。
您的代码大部分看起来都不错,尽管有些部分似乎来自较旧的教程。您应该进行一些更改,其中之一是您需要再次调用您的 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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
比使用委托方法显示恢复警报
/// 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.
}
Run Code Online (Sandbox Code Playgroud)
解锁产品只是一种方法,我相信您也已经拥有了。
func unlockProduct(withIdentifier productIdentifier: String) {
switch productIdentifier {
/// unlock product for correct ID
}
}
Run Code Online (Sandbox Code Playgroud)
作为旁注,你应该移动这条线
SKPaymentQueue.default().add(self)
Run Code Online (Sandbox Code Playgroud)
退出您的恢复和购买功能并将其放入 viewDidLoad。
Apple 建议您在应用启动后立即添加事务观察器,并且仅在应用关闭时将其删除。不幸的是,很多教程都没有正确地教你这一点。这样您就不能确定任何不完整的交易(例如由于网络错误)将始终正确恢复。
https://developer.apple.com/library/content/technotes/tn2387/_index.html
在我的实际项目中,我的 IAP 代码位于 Singleton 类中,因此我实际上会使用委托将 unlockProduct 方法转发到处理游戏数据的类。我还可以确保在应用程序启动时添加观察者。
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
2447 次 |
| 最近记录: |