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