How to restore your single In-App Purchase in iOS

- by

This is an addition to my previous article. To allow uses to restore their In-App Purchases you can expand on the previous article and include the following method in your Shop class. This needs to be public so it can be called from your main class:

- (BOOL)restoreThePurchase {
    
    // verify/refresh receipt here (even though Apple doesn't say how)
    
    // restore the purchase
    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
    
    return YES;
}

Much like submitting a payment, this now submits a “restore request” to the app store, which in turn will get back to us via the observer delegate. Here’s the implementation from before (still in AppDelegate.m), with the addition of a fleshed out reaction to the restore:

#pragma mark - StoreKit Observer

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased: {
                // user has purchased
                [self saveTransactionReceipt:transaction];
                [self unlockFullVersion];
                // download content here if necessary
                [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
                break;
            }
                
            case SKPaymentTransactionStateFailed: {
                // transaction didn't work
                [self displayAlertViewWithMessage:@"There was a problem with your purchase. Please try again later."];
                break;
            }
                
            case SKPaymentTransactionStateRestored: {
                // purchase has been restored
                [self displayAlertViewWithMessage:@"Successfully restored your purchase"];
                [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
                break;
            }
                
                
            case SKPaymentTransactionStatePurchasing: {
                // currently purchasing
                break;
            }
                
            default:
                break;
        }
    }
}

We let the user know about a successful restore (or failure) via an alert view. Since I’m calling this multiple times it was easier to create a method for this:

- (void)displayAlertViewWithMessage:(NSString *)message {
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}

Note that you should validate the purchase before restoring it. The Apple documentation goes on a bit about receipt validation without describing a way to actually do it.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.