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

Kat*_*ins 0 objective-c in-app-purchase ios swift

我有一些恢复交易仍然卡在我的付款队列中 - 因为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];
}];
Run Code Online (Sandbox Code Playgroud)

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

任何人都可以帮我做到这一点吗?谢谢 :-)

Cha*_*son 5

这是一个 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
            }
        }
Run Code Online (Sandbox Code Playgroud)

  • 如果您只是想清除当前卡住的失败事务,那么临时按钮是一个不错的解决方案。但是,我会在您的 SKProductsRequestDelegate updatedTransactions 方法中添加这样的代码。由于网络连接不良/资金不足等原因,处理应用内购买失败非常重要。此外,在循环中,您应该检查事务的状态,而不仅仅是完成每个事务。请参阅我更新的答案。 (3认同)