为什么将SKPaymentTransactionObserver添加到paymentQueue后会触发委托函数paymentQueue?

时间:2019-02-07 10:04:33

标签: ios swift storekit

我正在使用StoreKit框架来实现自动更新订阅。我的实现工作正常,但是PaymentQueue存在问题。当我将SKPaymentTransactionObserver添加到此队列时,它将触发与添加SKPayment时相同的委托函数。这可以避免吗?

在执行以下任一操作时触发的paymentQueue委托方法:

SKPaymentQueue.default().add(SKPaymentTransactionObserver)
SKPaymentQueue.default().add(SKPayment)

代理方法

// Observe transaction updates.
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    for transaction in transactions{
        //print("rawValue: \(transaction.transactionState.rawValue)")

        switch transaction.transactionState.rawValue{
        case 0:
            // DO NOT 'finishTransaction'
            return print("paymentQueue.purchasing")
        case 1:

            queue.finishTransaction(transaction)
            self.registerPurchaseSuccess()
            return print("paymentQueue.PURCHASE SUCCESS")

        case 2:

            if let error = transaction.error{
                queue.finishTransaction(transaction)
                return print("paymentQueue.FAIL.error: \(error.localizedDescription)")

            }else{

                queue.finishTransaction(transaction)
                return print("paymentQueue.fail. no errors")
            }


        case 3:
            queue.finishTransaction(transaction)
            return print("paymentQueue.pending.restored purchases")
        case 4:
            queue.finishTransaction(transaction)
            return print("paymentQueue.pending")


        default:
            return print("paymentQueue.unresolved issue ...")
        }

    }
}

成功将我的SKPaymentTransactionObserver添加到SKPaymentQueue后,我最终触发了'self.registerPurchaseSuccess()'-这是没有意义的,因为我还没有进行任何付款交易-只是在SKPaymentQueue中添加了一个观察者

0 个答案:

没有答案