Tag Archives: StoreKit

Creating Multiple In-App Purchases in iOS

In this screencast I’ll show you how to create multiple in-app purchases in you iOS app. The first part is a quick overview, and the second part shows how I’m tinkering with the code (members only).

This is based on an earlier tutorial in which I’m explaining how to create a single in-app purchase – if you’d like to follow along, you can find it here:

In a nutshell we’ll duplicate the earlier Shop class, add our new product identifier and amend the StoreKit observer method, as well as the alertView so that it can unlock the correct product.

The source code for this project is available on GitHub: the master branch is the earlier tutorial with a single product, and the Multi-IAP branch is what’s shown in this video.

This content is for members only.



Creating an In-App Purchase in iOS 7 and Xcode 5.1

In this 7-part screencast series I’ll show you how to create an In-App Purchase in iOS 7 with Xcode 5.1.

The course will run you through everything from setting up your product in iTunes Connect, creating a custom shop class for easy re-use, making “first contact” with the App Store and how to deal with its responses. I’ll explain the overall concept in Part 1.

Parts 1+2 are free to watch. The rest is for members only. Sign up here for full access to see the whole course.

Part 2 describes how to setup your your app for use with In-App purchases. We’ll setup a new App ID in Member Center, and create a product in iTunes Connect.

This content is for members only.

I’m following my earlier two articles almost to the letter, here they are for reference:





How to restore your single In-App Purchase in iOS

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.





How to create a single In-App Purchase in iOS 7

App Store IconThis “simple everyday” feature has caused me many headaches on many occasions. Implementing a simple unlockable feature isn’t as straightforward as it perhaps could be – and I’ll make it as simple as possible (so I won’t forget next time I need to add it to an app).

In this example we’re going to have one single feature that is already setup in iTunes Connect. It’s a non-consumable feature, such as “The Full Version”. To keep all our methods together in one place we’ll create a new NSObject class called “Shop”. This is where we’ll do most of the coding. Later we’ll also add code to the AppDelegate class.

This is a LONG article – grab a coffee and don’t get overwhelmed. It’ll take several tries to get comfortable with this matter (it’s not just you).

UPDATE: I’m explaining how to build this project step by step in a 7 part screencast – check it out here: http://pinkstone.co.uk/creating-an-in-app-purchase-in-ios-7-and-xcode-5-1/

Let’s take a look