添加和删除 SKPaymentQueue 观察者 Swift 的正确方法
我像这样在 didFinishLaunchingWithOptions
中添加观察者,然后调用收据验证:
IAPManager.shared.startObserving()
IAPManager.shared.IAPResponseCheck(iapReceiptValidationFrom: .didFinishLaunchingWithOptions)
并在 applicationWillTerminate
中删除它,如下所示:
IAPManager.shared.stopObserving()
我还在 applicationWillTerminate
中检查购买状态 < code>applicationWillEnterForeground 通过调用收据验证:
IAPManager.shared.IAPResponseCheck(iapReceiptValidationFrom: .applicationWillEnterForeground)
IAP Manager 类简而言之:
class IAPManager: NSObject {
static let shared = IAPManager()
private override init() { super.init() }
func startObserving() {
SKPaymentQueue.default().add(self)
}
func stopObserving() {
SKPaymentQueue.default().remove(self)
}
}
在 updatedTransactions
内部的 IAP 管理器类中,我正在验证收据,然后完成每次购买后的交易像这样恢复:
case .purchased:
self.IAPResponseCheck(iapReceiptValidationFrom: .purchaseButton)
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
totalRestoredPurchases += 1
SKPaymentQueue.default().finishTransaction(transaction)
最后在 paymentQueueRestoreCompletedTransactionsFinished 内部调用收据验证:
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
if totalRestoredPurchases != 0 {
self.IAPResponseCheck(iapReceiptValidationFrom: .restoreButton)
} else {
print("IAP: No purchases to restore!")
onBuyProductHandler?(.success(false))
}
}
但是当我从后台返回前台或重新启动应用程序时,我的收据验证 Func 会被随机调用多次。
func IAPResponseCheck(iapReceiptValidationFrom: IAPReceiptValidationFrom) {
print("iapReceiptValidationFrom \(iapReceiptValidationFrom)")
}
我在互联网上搜索发现它正在发生,因为不知何故添加了多个观察者。但我是根据苹果的指导方针添加的。那么根据我的实现,我在这里缺少什么?
我只想调用一次收据验证函数
I am adding the observer in the didFinishLaunchingWithOptions
like this and then call receipt validation:
IAPManager.shared.startObserving()
IAPManager.shared.IAPResponseCheck(iapReceiptValidationFrom: .didFinishLaunchingWithOptions)
And removing it in the applicationWillTerminate
like this:
IAPManager.shared.stopObserving()
I am also checking the state of the purchase at applicationWillEnterForeground
by calling the receipt validation:
IAPManager.shared.IAPResponseCheck(iapReceiptValidationFrom: .applicationWillEnterForeground)
IAP Manager class in short:
class IAPManager: NSObject {
static let shared = IAPManager()
private override init() { super.init() }
func startObserving() {
SKPaymentQueue.default().add(self)
}
func stopObserving() {
SKPaymentQueue.default().remove(self)
}
}
In the IAP manager class inside of updatedTransactions
, I am verifying the receipt and then finishing the transaction after each purchase & restore like this:
case .purchased:
self.IAPResponseCheck(iapReceiptValidationFrom: .purchaseButton)
SKPaymentQueue.default().finishTransaction(transaction)
case .restored:
totalRestoredPurchases += 1
SKPaymentQueue.default().finishTransaction(transaction)
And lastly call the receipt validation inside of paymentQueueRestoreCompletedTransactionsFinished
:
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
if totalRestoredPurchases != 0 {
self.IAPResponseCheck(iapReceiptValidationFrom: .restoreButton)
} else {
print("IAP: No purchases to restore!")
onBuyProductHandler?(.success(false))
}
}
But my receipt validation Func gets called multiple times randomly when I came back from background to foreground, or restart the app.
func IAPResponseCheck(iapReceiptValidationFrom: IAPReceiptValidationFrom) {
print("iapReceiptValidationFrom \(iapReceiptValidationFrom)")
}
I search on the internet I found that it is happening because somehow there are multiple observers are added. But I add it according to apple's guidelines. So according to my implementation what am I missing here?
I want to call my receipt validation func just one time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论