SKPaymentQueue.defaultQueue (). AddPayment Fails when moving between VC Swift

IAP works, but when I move between views and return to IAP VC, the application crashes in SKPaymentQueue.defaultQueue().addPayment(payment) ERROR: EXC_BAD_ACCESS

+10
ios swift storekit in-app-purchase
source share
2 answers

I found a solution, I need to remove SKPaymentQueue in viewWillDisappear

Swift 4.2 Update

 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) } 
+44
source share

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) } } 
+1
source share

All Articles