Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27191

in app purchase restore feature

Anybody know how to make restore option using IAP.

I use non consumable product for purchase.

I know that I have to implement this delegate methods:

- (void)restoreTransaction:(SKPaymentTransaction *)transaction
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue

but I still can not figure out with a process invoke this method.

I assume that I need to invoke this method [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; before I get invoke callback method.

Can you explain step by step how it work.

Upvotes: 2

Views: 2069

Answers (1)

joern
joern

Reputation: 27620

You assume right! The only thing you have to invoke is:

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

This restores all the completed transactions that the user has made. For each transaction this SKPaymentTransactionObserver method is called (the same method is also called each time a user makes a purchase):

    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
        for (SKPaymentTransaction *transaction in transactions) {
            switch (transaction.transactionState) {
                case SKPaymentTransactionStatePurchased:
                    [self completeTransaction:transaction];
                    break;
                case SKPaymentTransactionStateFailed:
                    [self failedTransaction:transaction];
                    break;
                case SKPaymentTransactionStateRestored:
                    [self restoreTransaction:transaction];
                    break;              
                default:
                    break;
            }
        }

    }

Using the transactionState you can distinguish whether the transaction was a original purchase (SKPaymentTransactionStatePurchased) or a restore (SKPaymentTransactionStateRestored) if you need to do that.

If you need to know when the restore is finished you can use:

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
   NSLog(@"%d items restored", queue.transactions.count);
}

Upvotes: 4

Related Questions