SKPaymentQueue.defaultQueue().addPayment(payment) 在 VC Swift 之间移动时崩溃

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

IAP 正在运行,但是当我在视图之间移动并返回 IAP VC 时,应用程序在 SKPaymentQueue.defaultQueue().addPayment(payment) ERROR: EXC_BAD_ACCESS

时崩溃

我找到了解决方案,您需要清理 viewWillDisappear

中的 SKPaymentQueue

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

我使用 class 而不是 viewController。

import StoreKit

open class IAPHelper: NSObject  {

     //Properties

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

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