StoreKit Testing in Xcode 12: Getting Started
Learn to use the new StoreKit testing framework to exercise your in-app purchases right inside Xcode. By Saleh Albuga.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
StoreKit Testing in Xcode 12: Getting Started
30 mins
- Getting Started
- Creating StoreKit Configuration Files
- Adding Salad Recipes
- Adding Healthy Coins
- Adding Newsletter Subscriptions
- Testing in Xcode During Development
- Testing Non-Consumables
- Simulating Ask to Buy
- Testing Consumables
- Simulating Other Storefronts
- Testing Subscriptions
- Deleting Transactions
- Deleting a Subscription
- Simulating Refunds
- Comparing Testing in Xcode to The Sandbox Environment
- Automating StoreKitTest Tests
- Testing Receipt Validation
- Starting Automated Testing
- Testing First Time Purchases
- Testing Interrupted Purchases
- Testing Subscriptions
- Where to Go From Here?
While building iOS apps is a fun process, testing in-app purchases, or IAPs, can be finicky and prone to errors. You used to first create sandbox testers, then log into your testing devices to test purchases in the sandbox environment. A tedious process that often caused unnecessary frustrations.
With Xcode 12, Apple introduced local StoreKit testing in Xcode. Now you can test different IAP scenarios without connecting to the App Store servers! Instead, you only need to set up a local testing environment in Xcode.
In this tutorial, you’ll:
- Work with StoreKit Configuration Files
- Test making different kinds of purchases locally in Xcode
- Manage transactions made in the Xcode environment
- Use the StoreKitTest framework to automate testing common scenarios
For this tutorial, you should be familiar with IAPs and StoreKit.
Getting Started
Download the starter project by clicking the Download Materials button at the top or bottom of the tutorial. Open the project. Build and run.
It doesn’t do much now, but it’s going to be delicious!
You’ll work on a salad recipe app called GreenBar. It lets users purchase salad recipes and award Healthy Coins to the author when they like a recipe.
Switch to the Coins tab.
You don’t have any coins yet but you will soon!
The app has three main views: ProductsView.swift, RecipeView.swift and CoinsView.swift.
-
ProductsView
is the menu where users view and purchase recipes. -
RecipeView
displays a purchased recipe’s content. -
CoinsView
shows the coins balance and bundles available for purchase.
In the GreenBar folder, you’ll also find GreenBarContent.swift and IAPHelper.swift.
-
GreenBarContent
is a simple struct with information about the recipes sold in the app. -
IAPHelper
calls StoreKit methods and implements its delegate methods. It has everything you need to handle IAPs.
Now check out the Receipt folder. You’ll see IAPReceipt.swift, Receipt.swift and ASN1Helpers.swift. They have everything you need to load an IAP receipt, read its content and validate it locally.
You’ll find GreenBar products already defined in the code. Open GreenBarContent.swift and check the definitions at the top.
// Recipes, non-consumables
public static let caesarSalad = "com.raywenderlich.GreenBar.recipes.caesar"
public static let easyPastaSalad =
"com.raywenderlich.GreenBar.recipes.easypasta"
public static let healthyTacoSalad =
"com.raywenderlich.GreenBar.recipes.healthytaco"
public static let tartCherrySalad =
"com.raywenderlich.GreenBar.recipes.tartcherrysalad"
// Coins, consumables
public static let coins10 = "com.raywenderlich.GreenBar.coins.10"
public static let coins20 = "com.raywenderlich.GreenBar.coins.20"
// Subscriptions
public static let greenTimes =
"com.raywenderlich.GreenBar.newsletters.greentimes"
public static let ketoNews = "com.raywenderlich.GreenBar.newsletters.ketonews"
These are the IAP’s product IDs. They’re organized in three groups:
- Recipes, non-consumables
- Coins, consumables
- Subscriptions
With the starter project covered, you’ll move on to creating StoreKit configuration files.
Creating StoreKit Configuration Files
Before you test IAPs in Xcode, you need to set up the local environment where you’ll make purchases without connecting to the App Store servers. StoreKit configuration files contain the IAPs and data you’d enter in App Store Connect when you create an IAP. This data includes name, product ID, price, localizations and subscription duration.
Data in StoreKit configuration files are independent of App Store Connect data. You can also mark non-consumable IAPs for Family Sharing. Additionally, you can set up introductory or promotional offers for auto-renewable subscriptions.
You’ll see more scenarios later. Now, it’s time to add products to the app.
Adding Salad Recipes
Select the GreenBar group in Project navigator. Next, create a new group by selecting File ▸ New ▸ Group. Name your group StoreKit.
This is where you’ll put your StoreKit configuration files.
Now create your first StoreKit configuration. Select your group and choose File ▸ New ▸ File…. Look for StoreKit Configuration File in the menu of file types.
Name the file RecipesAndCoins.storekit. Then, select GreenBar as the target and click Create. Open the file and click + in the bottom left corner to add a new StoreKit item.
Select Add Non-Consumable In-App Purchase. As you can see, Xcode created a new IAP.
The salad recipes are non-consumables because users can purchase them once and restore them later. You’ll start by creating the recipes’ IAPs. Look at the recipes’ product IDs in the first group of definitions in GreenBarContent.swift:
// Recipes, non-consumables
public static let caesarSalad = "com.raywenderlich.GreenBar.recipes.caesar"
public static let easyPastaSalad =
"com.raywenderlich.GreenBar.recipes.easypasta"
public static let healthyTacoSalad =
"com.raywenderlich.GreenBar.recipes.healthytaco"
public static let tartCherrySalad =
"com.raywenderlich.GreenBar.recipes.tartcherrysalad"
GreenBar’s product IDs follow this convention:
com.raywenderlich.GreenBar.[IAPType].[itemID]
Go to RecipesAndCoins.storekit. Fill the following information for CaesarSalad in your new IAP:
- Reference Name: CaesarSalad
- Product ID: com.raywenderlich.GreenBar.recipes.caesar
- Price: 0.99
- Display Name: Caesar Salad
- Description: The Classic Caesar Salad
Now, one by one, add the rest of the recipes as Non-Consumable In-App Purchases with the following information:
Easy Pasta Salad
- Reference Name: EasyPastaSalad
- Product ID: com.raywenderlich.GreenBar.recipes.easypasta
- Price: 0.99
- Display Name: Easy Pasta Salad
- Description: Light and Easy!
Healthy Taco Salad
- Reference Name: HealthyTacoSalad
- Product ID: com.raywenderlich.GreenBar.recipes.healthytaco
- Price: 0.99
- Display Name: Healthy Taco Salad
- Description: A Healthy and Delicious Taco in a Bowl
Tart Cherry Salad
- Reference Name: TartCherrySalad
- Product ID: com.raywenderlich.GreenBar.recipes.tartcherrysalad
- Price: 0.99
- Display Name: Tart Cherry Salad
- Description: Sweet, Refreshing and Finely Chopped!
With the recipes in place, you’ll now add healthy coins.
Adding Healthy Coins
Check the second group of product IDs in GreenBarContent.swift:
// Coins, consumables
public static let coins10 = "com.raywenderlich.GreenBar.coins.10"
public static let coins20 = "com.raywenderlich.GreenBar.coins.20"
GreenBar users can purchase ten Coins for $4.99 or 20 coins for $8.99. They can give recipe authors coins if they like a recipe.
In RecipesAndCoins.storekit, add the following Consumable IAPs:
10 Coins
- Reference Name: 10Coins
- Product ID: com.raywenderlich.GreenBar.coins.10
- Price: 4.99
- Display Name: 10 Healthy Coins
20 Coins
- Reference Name: 20Coins
- Product ID: com.raywenderlich.GreenBar.coins.20
- Price: 8.99
- Display Name: 20 Healthy Coins
You’ve now added both recipes and coins to RecipesAndCoins.storekit. Next, you’ll add the last purchase type: a subscription.
Adding Newsletter Subscriptions
You’ll add the subscriptions in a separate StoreKit configuration file. This makes testing the app’s IAP logic easier.
First, create a new StoreKit configuration file in the same StoreKit group. Name it Subscriptions.storekit. As before, select GreenBar as the target.
Go back to GreenBarContent.swift and check the third group of product IDs:
// Subscriptions
public static let greenTimes =
"com.raywenderlich.GreenBar.newsletters.greentimes"
public static let ketoNews = "com.raywenderlich.GreenBar.newsletters.ketonews"
These are newsletters GreenBar offers users. Click + in the bottom left corner and select Add Auto-Renewable Subscription.
Xcode prompts you to add a subscription group the first time you add an auto-renewable subscription.
Name the group Newsletters and click Done. Now, Xcode will create a group with a new subscription item.
Select it and fill the GreenTimes newsletter details below:
- Reference Name: GreenTimes
- Product ID: com.raywenderlich.GreenBar.newsletters.greentimes
- Price: 11.99
- Subscription Duration: 1 Month
- Display Name: GreenTimes
- Description: The lovely curated monthly newsletter
Add another auto-renewable subscription in the same subscription group for the KetoNews newsletter:
- Reference Name: KetoNews
- Product ID: com.raywenderlich.GreenBar.newsletters.ketonews
- Price: 6.99
- Subscription Duration: 1 Month
- Display Name: KetoNews
- Description: KetoNews Newsletter
With all the products in place, it’s time to start testing in Xcode.