iosobjective-cin-app-purchasein-app

how to Restore IAP for non-consumable product in iOS


I want to know that do one have to put two buttons separate one for Upgrade and Other for Restore for restore previous transactions??

What steps should be taken by my self to avoid reject chances of App for Restore transaction.

I used below code for restore non-consumable In App Purchase. If any changes in below code please let me know.

- (void)buyProduct:(SKProduct *)product {

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        //[[SKPaymentQueue defaultQueue] addPayment:payment];
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{

    for (SKPaymentTransaction * transaction in transactions) {

        switch (transaction.transactionState)
        {
                // Call the appropriate custom method for the transaction state.
            case SKPaymentTransactionStatePurchasing:
                [self showTransactionAsInProgress:transaction deferred:NO];
                break;
            case SKPaymentTransactionStateDeferred:
                [self showTransactionAsInProgress:transaction deferred:YES];
                break;
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                // For debugging
                NSLog(@"Unexpected transaction state %@", @(transaction.transactionState));
                break;
        }
    };


}

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
    NSLog(@"Restore Completed Transactions Failed WithError...%@",error);
    [self failedMessage:@"Restore Completed Transactions Failed"];
    [self stopIndicator];
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSMutableArray *arrPurchasedItemIDs = [[NSMutableArray alloc] init];
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        NSString *productID = transaction.payment.productIdentifier;

        [arrPurchasedItemIDs addObject:productID];

        NSLog(@"arrPurchasedItemIDs : %@",arrPurchasedItemIDs);
    }

    NSLog(@"Restore Completed");
    [self completeMessage:@"Restore Completed"];
    [self stopIndicator];
}

Solution

  • Here how you can separate the Restore purchase code :

    -(void)doClickRestore {
        [APP_DEL doStartSpinner];
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }
    
    -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
        for (SKPaymentTransaction *transaction in transactions) {
            switch (transaction.transactionState) {
                    // Purchase success Transaction
                case SKPaymentTransactionStatePurchased:
                    [self completeTransaction:transaction];
                    break;
                    // Purchase fail Transaction
                case SKPaymentTransactionStateFailed:
                    [self failedTransaction:transaction];
                    break;
                case SKPaymentTransactionStateRestored:
                {
                    [self doStopSpinner];
                    [self restoreTransaction:transaction];
                }
                default:
                    break;
            }
        }
    }
    
    - (void) restoreTransaction: (SKPaymentTransaction *)transaction {
        [self doStopSpinner];
        [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }
    

    Note :

    Clicking on Restore purchases will restore all purchased non consumable inApp purchases

    Hope it will helps.

    These below two mehods are optional but if you want you can use it.

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