Article

Loading In-App Product Identifiers

Load the unique identifiers for your in-app products in order to retrieve product information from the App Store.

Overview

Implementing an in-app purchase flow can be divided into three stages. In the first stage of the purchase process, your app retrieves information about its products from the App Store, presents its store UI to the user, and lets the user select products. Your app requests payment when the user selects a product in your app's store, and finally, your app delivers the product. The steps performed by your app and the App Store in the first stage are highlighted in Figure 1.

Figure 1

Begin the purchase process by retrieving product information from the App Store

A flow chart depicting the steps of the in-app purchase process. The product information retrieval stage is diagrammed as three steps between your app and the App Store. First, your app makes a request for a product; the App Store provides the product information requested; and finally, your app displays its store UI with the product information.

To begin the purchase process, your app must know its product identifiers so it can retrieve information about the products from the App Store and present its store UI to the user. Every product sold in your app has a unique product identifier. You provide this value in App Store Connect when you create a new in-app purchase product (see Create an In-App Purchase for more information). Your app uses these product identifiers to fetch information about products available for sale in the App Store, such as pricing, and to submit payment requests when users purchase those products.

There are several strategies for storing a list of product identifiers in your app, such as embedding in the app bundle or storing on your server. You can then retrieve the product identifiers by reading them locally in the app bundle or fetching them from your server. Choose the method that best serves your app's needs.

Retrieve Product IDs from the App Bundle

Embed the product identifiers in your app bundle if:

  • Your app has a fixed list of in-app purchase products. For example, apps with an in-app purchase to remove ads or unlock functionality can embed the product identifier list in the app bundle.

  • You expect users to update the app in order to see new in-app purchase products.

  • The app or product does not require a server.

Include a property list file in your app bundle containing an array of product identifiers, such as the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>com.example.level1</string>
    <string>com.example.level2</string>
    <string>com.example.rocket_car</string>
</array>
</plist>

To get product identifiers from the property list, locate the file in the app bundle and read it.

NSURL *url = [[NSBundle mainBundle] URLForResource:@"product_ids"
                                     withExtension:@"plist"];
NSArray *productIdentifiers = [NSArray arrayWithContentsOfURL:url];

Retrieve Product IDs from Your Server

Store the product identifiers from your server if:

  • You update the list of in-app products frequently, without updating your app. For example, games that supports additional levels or characters should fetch the product identifiers list from your server.

  • The products consist of delivered content.

  • Your app or product requires a server.

Host a JSON file on your server with the product identifiers. For example, the following JSON file contains three product IDs:

[
    "com.example.level1",
    "com.example.level2",
    "com.example.rocket_car"
]

To get product identifiers from your server, fetch and read the JSON file.

- (void)fetchProductIdentifiersFromURL:(NSURL *)url delegate:(id)delegate
{
    dispatch_queue_t global_queue =
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSError *err;
        NSData *jsonData = [NSData dataWithContentsOfURL:url
                                                 options:NULL
                                                   error:&err];
        if (!jsonData) { /* Handle the error. */ }

        NSArray *productIdentifiers = [NSJSONSerialization
            JSONObjectWithData:jsonData options:NULL error:&err];
        if (!productIdentifiers) { /* Handle the error. */ }

        dispatch_queue_t main_queue = dispatch_get_main_queue();
        dispatch_async(main_queue, ^{
            [delegate displayProducts:productIdentifiers]; // Custom method.
        });
    });
}

Consider versioning the JSON file so that future versions of your app can change its structure without breaking older versions of your app. For example, you could name the file that uses the old structure products_v1.json and the file that uses a new structure products_v2.json. This is especially useful if your JSON file is more complex than the simple array in the example.

To ensure that your app remains responsive, use a background thread to download the JSON file and extract the list of product identifiers. To minimize the data transferred, use standard HTTP caching mechanisms, such as the Last-Modified and If-Modified-Since headers.

See Also

Retrieving Product Information

Fetching Product Information from the App Store

Retrieve up-to-date information about the products for sale in your app to display to the user.

SKProductsRequest

An object that can retrieve localized information from the App Store about a specified list of products.

SKProduct

Information about a product previously registered in App Store Connect.

SKProductsResponse

An App Store response to a request for information about a list of products.

SKProductSubscriptionPeriod

An object containing the subscription period duration information.

SKProductDiscount

The details of a discount offer for a subscription product.