0

我正在didFinishLaunchingWithOptions像这样添加观察者,然后调用收据验证:

IAPManager.shared.startObserving()
IAPManager.shared.IAPResponseCheck(iapReceiptValidationFrom: .didFinishLaunchingWithOptions)

并像这样删除它applicationWillTerminate

IAPManager.shared.stopObserving()

我还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)
    }
}

在内部的 IAP 管理器类中updatedTransactions,我正在验证收据,然后在每次购买和恢复后完成交易,如下所示:

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)")

}

我在互联网上搜索发现它正在发生,因为不知何故添加了多个观察者。但我根据苹果的指南添加它。所以根据我的实现,我在这里缺少什么?

我只想调用一次收据验证函数

4

0 回答 0