Sample Code

Offering Media for Sale in Your App

Allow users to purchase media in the App Store from within your app.



This sample code project shows how to make it possible for users to purchase media from your app using the SKStoreProductViewController class. Additionally, the sample demonstrates how to measure the effectiveness of a cross-promotional campaign for your apps or third-party apps using SKStoreProductViewController’s SKStoreProductParameterCampaignToken and SKStoreProductParameterProviderToken keys.

The app in this sample project displays a list of media items that the user can tap in order to make purchases from the App Store. You can also track advertising and promotions from this interactive list of media items for sale.

You must run this sample on an iOS device.

Get Started

This sample defines a Product data type to represent media information saved in the Products.plist file. Products.plist contains a single Product entry. Before you can run this sample on your device, you must update this entry with your own data.

  1. Set the sample’s productIdentifier key to your media’s iTunes identfier. For more information on how to get iTunes identifiers, see SKStoreProductParameterITunesItemIdentifier.

  2. Set the title key to your media’s name.

  3. Set the isApplication key to YES if your media is an app and NO otherwise.

  4. Set the campaignToken and providerToken keys to the appropriate tokens if you are cross-promoting apps and leave them empty if you are not. For more information on how to generate campaign and provider tokens, see Measure campaign performance, Campaign Token, and Provider Token.

To test more data, duplicate and update the Product entry in Products.plist as needed.

Display Media from the App Store

This sample launches to the TableViewController view controller that shows a list of media associated with iTunes identifiers saved in Products.plist. When the user taps any item in the list, TableViewController presents a page where they can purchase the media from the App Store. Use the following steps to set up the media list.

First, you must create a parameters dictionary with a SKStoreProductParameterItunesItemIdentifier key set to your media’s iTunes identifier (see SKStoreProductParameterITunesItemIdentifier for more information on how to find iTunes identifiers):

var parametersDictionary = [SKStoreProductParameterITunesItemIdentifier: product.productIdentifier]

Then create an SKStoreProductViewController object and set the view controller class as its delegate. Next, pass the dictionary to the loadProductWithParameters:completionBlock: method of the SKStoreProductViewController object:

// Create a store product view controller.
let store = SKStoreProductViewController()
store.delegate = self

Finally, present the SKStoreProductViewController object modally from your app:

 Attempt to load the selected product from the App Store. Display the store product view controller if success and print an error message,
store.loadProduct(withParameters: parametersDictionary, completionBlock: {[unowned self] (result: Bool, error: Error?) in
    if result {
        self.present(store, animated: true, completion: {
            print("The store view controller was presented.")
    } else {
        if let error = error {
            print("Error: \(error.localizedDescription)")

The delegate must dismiss the store view controller once the purchase completes or is canceled. TableViewController implements the SKStoreProductViewControllerDelegate method to dismiss the presented store product view controller. In your app, you can also use this method to resume any activities that were paused before presenting the store.

Track Advertising and Promotions

The SKStoreProductParameterCampaignToken and SKStoreProductParameterProviderToken keys allow you to track advertising and promotion for an app. When a user taps Find My Friends (with App Analytics campaign) in the list, TableViewController adds these keys and their values to a dictionary, which already contains SKStoreProductParameterItunesItemIdentifier set to its iTunes identifier.

When cross-promoting your apps, set SKStoreProductParameterProviderToken to your own provider token. When promoting apps for other developers, set it to their provider token.

TableViewController passes the dictionary to a created SKStoreProductViewController object, then proceeds to display the store product view controller modally, as discussed in the section Display Media from the App Store:

// Update `parametersDictionary` with the `campaignToken` and `providerToken` values if they exist and the specified `product` is an app.
if product.isApplication, !product.campaignToken.isEmpty, !product.providerToken.isEmpty {
    parametersDictionary[SKStoreProductParameterCampaignToken] = product.campaignToken
    parametersDictionary[SKStoreProductParameterProviderToken] = product.providerToken

See Also

Affiliate Recommendations


A view controller that provides a page where the user can purchase media from the App Store.


A class that validates advertiser-driven app installations.