Tal Zion
Tal Zion

Reputation: 6526

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

Upvotes: 11

Views: 3115

Answers (2)

Tal Zion
Tal Zion

Reputation: 6526

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

Upvotes: 45

Yurii Petrov
Yurii Petrov

Reputation: 341

I use my class instead of viewController.

import StoreKit

open class IAPHelper: NSObject  {

     //Properties

     public init() {
         //init properties
         super.init()
         SKPaymentQueue.default().add(self)
     }

     deinit {
         SKPaymentQueue.default().remove(self)
     }
}

Upvotes: 0

Related Questions