gpt4 book ai didi

ios - 使用 Swift 3 恢复应用内购买

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:11:45 24 4
gpt4 key购买 nike

我正在尝试在 Swift 3 中使用应用内购买。我可以在我的测试设备上成功付款。我在屏幕顶部有一个恢复按钮,点击后应该恢复所有以前的购买。这是我用来恢复的代码。我在函数中的队列总是空的。请帮我解决这个问题。

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
for transaction in queue.transactions{
let trans: SKPaymentTransaction = transaction as SKPaymentTransaction

let prodID = trans.payment.productIdentifier as String
switch prodID {
case "some_iap":
print("Do something")
finishTransaction(trans)

default: break
}
}
updatePurchases()
}

func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
print("error in restore")
}

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

for trans in queue.transactions{
let trans: SKPaymentTransaction = trans as SKPaymentTransaction
print(trans.transactionState.rawValue)
switch (trans.transactionState) {
case .purchased:
let prodID = p.productIdentifier as String
switch prodID {
case "some_iap":
print("do something")
updatePurchases()
finishTransaction(trans)

default:
print("defualt")
finishTransaction(trans)
}

case .failed: break

default: break
}
}
}

func finishTransaction(_ trans:SKPaymentTransaction){
SKPaymentQueue.default().finishTransaction(trans)
SKPaymentQueue.default().remove(self)
}

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

最佳答案

Swift 3 的解决方案

//Step 1 Call from button Restore Purchase
func restore() {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}

//Step 2 Get transactions
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

for transaction in transactions {

switch transaction.transactionState {
case .purchased:
print("Product Purchased")
unlockApp()
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)

break;
case .failed:
print("Purchased Failed");
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .restored:
print("Already Purchased")
unlockApp()

SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
default:
break
}
}
}




//If an error occurs, the code will go to this function
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
//Handle Error
}

关于ios - 使用 Swift 3 恢复应用内购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40941353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com