清除SKPAymentsQueue:强制完成未完成的交易

时间:2016-03-18 17:39:27

标签: ios objective-c swift in-app-purchase

我的某些还原交易仍然停留在我的付款队列中 - 因为在我测试有缺陷的恢复购买操作时,我从未使用该事务调用finishTransaction

从一些在线研究中,我意识到我必须在我的支付队列中手动强制完成未完成的交易。

有人在Objective-C中发布了这段代码:

// take current payment queue
SKPaymentQueue* currentQueue = [SKPaymentQueue defaultQueue];
// finish ALL transactions in queue
[currentQueue.transactions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[currentQueue finishTransaction:(SKPaymentTransaction *)obj];
}];

我不知道如何将其转换为Swift 2.0。

任何人都可以帮助我这样做吗?谢谢: - )

1 个答案:

答案 0 :(得分:2)

这是一个for循环,它将遍历每个待处理的事务并检查状态,并完成失败或成功购买的事务。

let currentQueue : SKPaymentQueue = SKPaymentQueue.default();
        for transaction in currentQueue.transactions {
            if (transaction.transactionState == SKPaymentTransactionState.failed) {
                //possibly handle the error
                currentQueue.finishTransaction(transaction);
            } else if (transaction.transactionState == SKPaymentTransactionState.purchased) {
                //deliver the content to the user
                currentQueue.finishTransaction(transaction);
            } else {
                //handle other transaction states
            }
        }