Step 2 Fetch Available InApp Purchase Items

With the In-App Purchase feature accessible, you can now check the App Store for the availability of your item. It is important to note that your application needs to already know the related In-App Purchase product ID before performing this check.

If you are offering only one or two In-App Purchase items with no plans on adding more, then the easiest thing to do is store your In-App Purchase product IDs within your app. Of course, any time you add a new In-App Purchase product ID to your app, you'll need to make it available to customers as an update, which will require submitting your app again for review/approval. Since iTunes Connect requires In-App Purchase product IDs to be unique, don't preassign nonexistent IDs in your parent app with plans to later create those IDs in iTunes Connect at some point in the future. That strategy could backfire on you if some other developer takes ownership of that product ID before you get a chance to create it in iTunes Connect. Of course, you could plan ahead by creating all your known In-App Purchase items in iTunes Connect now and simply mark them as "Not Ready for Sale." That way, you'll know the product IDs to include in your application binary, and when you're ready to make those future In-App Purchase items available to consumers, you can submit them for approval.

If you want the flexibility to add new In-App Purchase items without requiring a new build of your app, then you'll need to set up a mechanism on a remote server, where your app can fetch all related product IDs from your server. Store Kit does not provide any infrastructure for this, so it would be your responsibility to configure the app to talk to your server. Although this requires extra work on your part, it is the recommended method for retrieving product IDs, especially if you're constantly adding new In-App Purchase items. Apple's approval process is too time-consuming to continuously submit a new app update every time you release new purchasable add-on content.

The related In-App Purchase items will need to be approved and ready for sale in the App Store before customers can purchase them within your application. Before they are approved, they are available only to your In-App Purchase Test User account within your parent app's development build.

Once your app has retrieved the In-App Purchase Product ID—in this example, you're using only one—proceed to request that product from the App Store to ensure that it's currently available for purchase.

- (void)viewDidLoad {

if ([SKPaymentQueue canMakePayments]) {

// Yes, In-App Purchase is enabled on this device! // Proceed to fetch available In-App Purchase items.

// Replace "Your IAP Product ID" with your actual In-App Purchase Product ID, // fetched from either a remote server or stored locally within your app. SKProductsRequest *prodRequest= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"Your IAP Product ID"]]; prodRequest.delegate = self; [prodRequest start];

// Notify user that In-App Purchase is disabled via button text. [inappButton setTitle:@"In-App Purchase is Disabled" forState:UIControlStateNormal];

inappButton.enabled = NO;

[super viewDidLoad];

Of the new code added (in bold) to InAppPurchaseViewController.m's viewDidLoad, you'll need to replace the placeholder string "Your IAP Product ID" with your own actual In-App Purchase product ID.

And remember that SKProductsRequestDelegate code you added to the header file a while back? By assigning InAppPurchaseViewController as the SKProductsRequest delegate, you can simply state "self" for the Delegate property. The Store Kit will then return a response to this delegate. To receive that response, add the following code to the InAppPurchaseViewController.m file:

// StoreKit returns a response from an SKProductsRequest.

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

// Populate the inappBuy button with the received product info. SKProduct *validProduct = nil; int count = [response.products count]; if (count>0) {

validProduct = [response.products objectAtIndex:0];

if (!validProduct) {

[inappButton setTitle:@"No Products Available" forState:UIControlStateNormal];

inappButton.enabled = NO;

return;

NSString *buttonText = [[NSString alloc] initWithFormat:@"%@ - Buy %@", validProduct.localizedTitle, validProduct.price];

[inappButton setTitle:buttonText forState:UIControlStateNormal]; inappButton.enabled = YES; [buttonText release];

NSString *buttonText = [[NSString alloc] initWithFormat:@"%@ - Buy %@", validProduct.localizedTitle, validProduct.price];

[inappButton setTitle:buttonText forState:UIControlStateNormal]; inappButton.enabled = YES; [buttonText release];

Store Kit responds with an array of available In-App Purchase products. Since you have only one here, you populate the inappBuy button with the product's localized Title and Price (see Figure 6-13). If you're interested in also displaying the localized description, then that can be accessed via the localizedDescription property. Don't forget to enable the button via code now that it's displaying a valid In-App Purchase item.

Purchase Add-On Game Content

Super Powers Pack - Buy S1.99

Figure 6-13. The Store Kit replies to the product request with information on the available In-App Purchase item. The example code then populates the button with the item's localized title and price.

Why bother checking for the In-App Purchase item through the Store Kit if you already know the product ID? This vital step ensures that your application does not try to sell an item that is not currently available in the App Store. For example, what if you submitted both your application and an In-App Purchase item for review at the same time, but only your iPhone app was approved? By forcing your application to first check for the availability of the In-App Purchase item before listing it will prevent potential customer purchase issues. Never assume. Always perform this check before displaying any items for sale within your app.

If you successfully set up an In-App Purchase item in iTunes Connect and yet your app encounters problems retrieving that item data as a valid product, make sure your application's Bundle Identifier in its Info.plist file matches the parent app Bundle ID assigned to your In-App Purchase item in iTunes Connect.

Do not discard that In-App Purchase Product ID after using it for the product request. It'll come in handy again soon. If the user taps the buy button, you'll need that Product ID to submit a purchase request to Store Kit.

To retrieve valid product data from the App Store and successfully test In-App Purchase within your app, remember that your app must be either live in the App Store or in active review in iTunes Connect. As previously mentioned, if your app is currently unfinished, you can work around Apple's odd requirement by uploading an unfinished application binary to iTunes Connect and submitting your app for review. The important trick during the app submission process is to choose an availability date that is very far in the future so that your unfinished app does not accidentally appear in the App Store (in the off chance Apple approves it). While the app is in active review, you can proceed to test In-App Purchase within your app. Once you've completed testing, simply log back into iTunes Connect and reject the app, removing it from review. When your app is eventually finished and ready for the App Store, simply resubmit it via iTunes Connect with an adjusted availability date. For complete details on the app submission process, please see Chapter 9.

Continue reading here: Mining Additional Revenue with Affiliate Programs

Was this article helpful?

0 0

Readers' Questions

  • armida
    How to add items in itunesconnect inapp purchase product successfully?
    1 year ago