Logo for tanaschita.com

Get started with auto‑renewable subscriptions for iOS

Learn how to implement in-app purchase subscriptions with StoreKit and SwiftUI.

11 Dec 2023 · 5 min read

Apple provides 4 different types of in-app purchases that allow us to offer revenue generating content and features to users:

  • Consumable for recurring purchases that users can consume.
  • Non-consumable for one-time purchases that don’t expire.
  • Auto‑renewable subscriptions for subscriptions that renew automatically at the end of each subscription period until the user cancels.
  • Non-renewing subscriptions for subscriptions that expire after a fixed duration and require the user to manually renew them.

In this article, we'll take a closer look at auto‑renewable subscriptions, on how they work and how we can implement them using StoreKit and SwiftUI.

Let's get started.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Overview

Auto‑renewable subscriptions allow users to pay for access to premium features or content on a recurring basis, typically on a monthly or yearly basis. They are perfect for apps that offer ongoing access to premium content, services or features.

To implement auto‑renewable subscriptions, the following basic steps are required:

  1. Configure subscriptions in App Store Connect or through a local StoreKit configuration file for testing.
  2. Load and show subscriptions options in the iOS application.
  3. Unlock content when a user subscribes.
  4. Add support for restoring in-app purchases.

Let's look at each step in more detail.

1. Configure auto‑renewable subscriptions

To configure suscriptions through App Store Connect, we need an Apple Developer Account and a configured app in App Store Connect. For testing purposes, we can alternatively setup a StoreKit configuration file locally in Xcode which we'll do in this article. To learn more about StoreKit configuration files, check out this article on StoreKit Testing in Xcode.

Every subscription is part of a group. Users can only subscribe to one subscription of the same group at a time, but can change to another subscription in the same group. When adding our first auto-renewable subscription, we'll be asked to enter a group name.

Auto renewable subscritions setup example.

The setup above shows a subscription group named subscriptionOptions which contains two different subscription options: a premium.yearly and a premium.monthly subscription.

For every subscription option, we can setup a price, duration, free trial periods and more. To learn more on how to setup and display a free trial period, check out this article on introductory offers in StoreKit 2.

2. Load and show subscriptions options in the iOS application

After setting up subscrition options, we can load and show subscriptions options in our iOS application. In StoreKit 2, a subscription is represented by the Product struct. We can load products by calling its products(for:) method:

let subscriptions = try await Product.products(for: ["premium.yearly", "premium.monthly"])

Each product has properties which we can use to display subscription infos to the user. For example:

3. Unlock content when users subscribe

When a user decides to subscribe to one of the offered subscriptions, we can use the purchase method on the selected product:

let result = try await product.purchase()

This call will trigger Apple's native purchasing dialog providing us with the result after the user completes:

switch result {
case .success(let result):
switch verificationResult {
case .verified(let transaction):
// Give the user access to purchased content.
// Complete the transaction with transaction.finish()
case .unverified(let transaction, let verificationError):
// The purchase failed verification for the provided reason.
}
case .pending:
// The purchase is waiting for some user action.
// Purchase may succeed in the future,
// the resulting Transaction will be delivered via Transaction.updates
case .userCancelled:
// The user cancelled the purchase.
default:
break
}

4. Add support for restoring in-app purchases

When users reinstall our iOS app or download it on a new device, we want to make sure all in-app purchases the user previously made are restored. Restoring subscriptions works the same way as for any other StoreKit 2 products. Check out this article on how to restore in-app purchases with StoreKit 2 to learn more.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

storekit

swift

ios

How to implemet a free trial period for StoreKit 2 subscriptions in iOS

Learn how to implement StoreKit 2 introductory offers.

13 Nov 2023 · 2 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum