我目前正在使用沙盒用户开发和测试我的应用。即使事务已经完成,当我尝试Restore Purchases
时,我从队列中获得了多达32个旧事务。
基本上我想在Your purchase is being restored. Upon completion this dialog will close.
中提醒并在它完成时将其解雇。
private func showRestoreInProgressAlert() {
let alert = UIAlertController(title: "Restoring Purchase", message: "Your purchase history is being restored. Upon completion this dialog will close.", preferredStyle: .alert)
present(alert, animated: true, completion: nil)
NotificationCenter.default.addObserver(self, selector: #selector(dismissRestoreInProgressAlert(notification:)), name: SubscriptionService.restoreSuccessfulNotification, object: nil)
}
您可以从SKPaymentTransactionObserver
识别以下方法。一旦发送通知.restoreSuccessfulNotification
,警报将按预期被解除。但由于队列中有32个事务,弹出窗口不断出现并消失32次。
func handleRestoredState(for transaction: SKPaymentTransaction, in queue: SKPaymentQueue) {
print("Purchase restored for product id: \(transaction.payment.productIdentifier)")
queue.finishTransaction(transaction)
SubscriptionService.shared.uploadReceipt { (success) in
DispatchQueue.main.async {
NotificationCenter.default.post(name: SubscriptionService.restoreSuccessfulNotification, object: nil)
}
}
}
func paymentQueue(_ queue: SKPaymentQueue,
updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
handlePurchasingState(for: transaction, in: queue)
case .purchased:
handlePurchasedState(for: transaction, in: queue)
case .restored:
handleRestoredState(for: transaction, in: queue)
case .failed:
handleFailedState(for: transaction, in: queue)
case .deferred:
handleDeferredState(for: transaction, in: queue)
}
}
}
我已经完成了handlePurchasedState
和handleRestoredState
的交易,如下所示:
queue.finishTransaction(transaction)
那么,为什么每当我点击恢复购买时,我仍然会在队列中放置这么多旧交易?
UPDATE
这可能确实是沙箱的问题。
我试图做一个计数,但这没有帮助,因为并非所有这些交易似乎都是可恢复的。
我做了“硬重置”:
for transaction: AnyObject in SKPaymentQueue.default().transactions {
guard let currentTransaction: SKPaymentTransaction = transaction as? SKPaymentTransaction else {return}
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
}
现在似乎交易缓慢减少到零,这意味着Sandbox正在失去它们。
为什么SKPaymentQueue.default().finishTransaction()
工作,但queue.finishTransaction()
没有?我应该重构我的代码以使用SKPaymentQueue.default().finishTransaction()
而不是安全吗?或者这只是IAP沙箱的糟糕日子?
恢复将所有购买交付给您的交易观察员代表。这是预期的行为。
但是,以您想要的方式显示警报非常简单。
paymentQueueRestoreCompletedTransactionsFinished
委托方法。在此方法中,您可以关闭警报。每次请求还原时,都将返回所有活动订阅和所有非消耗性项目(由当前用户购买)。
想一想,如果你完成后不再返回,你将如何恢复?
显然,您的应用可以为用户提供多个有效订阅和/或非消耗品。为避免收到多个警报,您应该将所有StoreKit恢复回调组合成一个(或几个)用户通知。
这在你的情况下有意义吗?