问题
I have some restore transactions still stuck in my payments queue - because I never called finishTransaction
with the transaction once it was restored when I was testing a flawed restore purchases action.
From some online research, I realise I have to manually force unfinished transactions in my payments queue to finish.
Someone posted this code in 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];
}];
I have no idea how to convert it to Swift 2.0.
Can anyone pls help me do this? Thanks :-)
回答1:
Here is a for loop that will iterate through each pending transaction and check the state, and complete transactions that have either failed or successfully purchased.
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
}
}
来源:https://stackoverflow.com/questions/36090901/clearing-skpaymentsqueue-forcing-unfinished-transactions-to-finish