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.

4.6 (9) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 2 of 5 of this article. Click here to view the first page.

Testing in Xcode During Development

You added all the products in StoreKit configuration files. Before testing, you need to learn a couple of settings.

First, click the scheme GreenBarEdit Scheme. In the Scheme window, select Run on the left. Then select Options at the top.

Scheme editor

See the new StoreKit Configuration option. That’s where you’ll set the StoreKit configuration file you want to test with. Click its combo box.

StoreKit Configuration file option

You’ll see the StoreKit configuration files you created!

Once you set a configuration file, StoreKit uses the data in that file when your app calls StoreKit APIs instead of communicating with the App Store servers. For now, leave the option as None.

Note: To disable StoreKit testing in Xcode, set the StoreKit Configuration option to None.

Close the scheme editor.

Then, open one of the StoreKit configuration files and click Editor.

Editor menu shows StoreKit Configuration file options

Here you can configure the local environment to test different IAP scenarios like:

  • Test with a different storefront to see how your app looks and behaves with prices in different currencies.
  • Test with a different localization to see how the description and title look in other localizations.
  • Simulate Ask to Buy when purchasing from a family subscription with parental approval.
  • Simulate different StoreKit transaction errors and how your app handles them.
  • Change subscriptions expiry time rate.
  • Simulate interrupted purchases. These are transactions that require users to perform an action related to their Apple ID before making a purchase.

Finally, go to DebugStoreKitManage Transactions….

Transaction manager

This is the transaction manager where you’ll see all the transactions made in Xcode. You can use the transaction manager to:

  • Delete transactions: Made a transaction and found a bug in your code? Delete and purchase again it like it never happened.
  • Refund transactions
  • Approve or decline transactions when Ask to Buy is enabled.
  • Resolve transactions: Simulate users resolving an interrupted purchase.

Testing Non-Consumables

Set RecipesAndCoins.storekit as the active StoreKit Configuration in the scheme as you saw above.

StoreKit Configuration file selected in scheme editor

Now build and run.

GreenBar app Products view showing recipes

You can see the recipes you’ve added! StoreKit now uses RecipesAndCoins.storekit as the back end instead of the sandbox environment.

Now, buy the Easy Pasta Salad.

App Store confirmation sheet

Then, tap Confirm.

Purchase successful

You got the confirmation, have the recipe and the app made it available for you! Note the alert is showing Xcode as the environment. Tap the recipe to open it.

Recipe content

To see the transaction, open the transaction manager from DebugStoreKitManage Transactions…

Transaction manager showing the first transaction

The transaction manager shows the purchase you made as well as its type and time.

You successfully tested your first purchase in Xcode! Because you were able to access the recipe content, you now know GreenBar is handling purchases as it should.

Next, you’ll add a useful feature for parents.

Simulating Ask to Buy

With the introduction of Ask to Buy, family organizers can control Family Sharing for any non-adult family members. When kids want to buy a new item, they send a request to their family organizer who then approves or declines the requests. When testing your app, you should consider this a scenario.

First, open RecipesAndCoins.storekit. Then select EditorEnable Ask to Buy from the menu.

Editor menu showing Ask to Buy option

Next, build and run. Buy the Healthy Taco Salad and tap Confirm in the App Store sheet.

App showing Ask to Buy alert

Note: You need to build and run to apply the changes every time you change the local environment settings in the Editor menu.

You got the alert kids would see when buying items if they have Ask to Buy enabled. Tap Ask and open the transactions manager again.

Transaction manager showing the pending transaction

The transaction shows as Pending Approval. Look at the transaction controls at the top right:

Transaction manager controls

These controls let you manage transactions in Xcode. They’re enabled or disabled based on the transaction state.

For example, Approve Transactions and Decline Transactions are enabled because this transaction is pending approval. But Refund Purchases and Resolve Issues are disabled because this transaction isn’t complete yet. Delete Transactions is also enabled.

Since this is a testing environment, you can approve your requests. :] Click Approve Transactions and switch back to the simulator.

App showing the content unlocked after transaction approval

The app received the notification about the completed transaction and unlocked the recipe!

Take a moment to appreciate how easy it was to test this otherwise complex flow. Awesome, right?

Next, you’ll test consumables by buying and awarding coins.

Testing Consumables

Did you like the healthy taco salad? Then it’s time to support giving the author some coins for their hard work!

First, you need to disable Ask to Buy. Open the configuration file and select EditorDisable Ask to Buy.

Then build and run. Next, switch to the Coins tab.

GreenBar app Coins view

You don’t have any coins at the moment so buy 10 Healthy Coins.

Coins view after the transaction showing 10 coins balance

The app updates the Coins Balance to show you now have ten coins! Note how you can buy the same IAP several times since it’s a consumable.

Switch to the Products tab and open the Healthy Taco Salad recipe.

Recipe content view

Tap Give 5 Coins to give the author five healthy coins!

Coins given confirmation alert

Now, switch back to the Coins tab.

Coins view showing 5 coins left after giving 5

You have five coins left. Now you know the app is appropriately handling coin purchases. When you give coins, the app deducts them from your balance and displays the new balance.

Open the transaction manager to see this transaction.

Transaction manager

Next, you’ll learn how to simulate other storefronts to test the experience of purchasing in different countries.

Simulating Other Storefronts

When you added the products to StoreKit configuration files, you entered the prices without selecting a currency. That’s because the price is a testing value that’s not connected to price tiers in the App Store or real pricing information. However, StoreKit displays the correct currency symbols for the storefront you’re testing.

You can change the default storefront in the local environment.

Open RecipesAndCoins.storekit and select EditorDefault Storefront. Select Spain (EUR). Build and run.

Products view showing prices in Euros

The prices now display in Euros.

Before continuing, change it back to United States (USD) again.

Next, you’ll focus on testing subscriptions.

Testing Subscriptions

First, change the active StoreKit configuration file in the scheme to Subscriptions.storekit.

Scheme editor StoreKit configuration option

Then, build and run.

Note: You can do all of this in a single step by Option-clicking the Run button or by pressing Option-Command-R.

The app now shows the newsletter subscriptions instead of the recipes because you changed the configuration file.

Products view showing newsletter subscriptions

Note: Having multiple StoreKit configuration files can make testing during development easier. For example, you can split products into multiple configuration files when testing different IAPs or introducing new IAPs that need different handling.

Now, subscribe to GreenTimes.

App Store confirmation sheet

Tap Confirm.

Products view showing GreenTimes unlocked after the successful purchase

You’ve subscribed to the GreenTimes newsletter! Open the transaction manager to see the transaction. From here you can also see Subscription Options to further debug subscription flows.

Transaction manager

Next, you’ll learn how to delete transactions.