Search code examples
iosswiftin-app-purchasestorekit

SKPaymentQueue.defaultQueue().addPayment(payment) Crash when moving between VC Swift


The IAP is working, but when I move between views and come back to the IAP VC, the app crashes in SKPaymentQueue.defaultQueue().addPayment(payment) ERROR: EXC_BAD_ACCESS


Solution

  • I found the solution, you need to clean the SKPaymentQueue in the viewWillDisappear

    Update Swift 4.x

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        SKPaymentQueue.default().remove(self)
    }
    

    Swift 2.3

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated: animated)
        SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
    }