添加和删​​除 SKPaymentQueue 观察者 Swift 的正确方法

发布于 2025-01-10 15:16:33 字数 1986 浏览 1 评论 0原文

我像这样在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文