我在 Swift 4/iOS 11 中恢复 IAP 时遇到问题。我的AppDelegate
实施SKPaymentTransactionObserver
. In AppDelegate
's didFinishLaunchingWithOptions
我正在打电话SKPaymentQueue.default().add(self)
。我实施paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
AppDelegate 中也有这个函数。
From a ViewController
我可以这样购买:
let payment = SKPayment(product: products.first!)
SKPaymentQueue.default().add(payment)
购买成功完成 - 我收到updatedTransaction
与状态.purchased
in my paymentQueue
观察者。
但是,当我从同一个调用以下内容时ViewController
为了恢复购买:
SKPaymentQueue.default().restoreCompletedTransactions()
the paymentQueue updatedTransactions
函数永远不会被调用。
看来我的沙箱用户等设置正确,因为初始购买正在运行。沙盒用户有能力恢复购买吗?除了打电话之外我还需要做任何额外的工作吗restoreCompletedTransactions()
恢复购买?
更新一下,这是我的paymentQueue updatedTransactions
存在于的代码AppDelegate
:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions{
switch transaction.transactionState{
case .purchased:
debugPrint("purchased it!")
menuDelegate?.unlockMenu()
//unlock the content, then
SKPaymentQueue.default().finishTransaction(transaction)
break
case .failed:
debugPrint("failed")
SKPaymentQueue.default().finishTransaction(transaction)
break
case .restored:
debugPrint("restored")
menuDelegate?.unlockMenu()
//unlock the content, then
SKPaymentQueue.default().finishTransaction(transaction)
break
case .purchasing:
debugPrint("purchasing...")
break
case .deferred:
debugPrint("deferred...")
break
}
}
}