Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ios - Clearing SKPAymentsQueue : Forcing Unfinished Transactions To Finish

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...