# How to add subscriptions to a Swift app
Table of Contents
These notes are based on the YouTube video by RevenueCat
Implementing In-App Subscriptions with RevenueCat
Prerequisites
To follow along with this tutorial, you’ll need:
- A RevenueCat account
- An app configured in the App Store Connect
- An app configured in RevenueCat
- Configured products in App Store Connect
- A sandbox tester account
Configuring RevenueCat
Before implementing in-app subscriptions, you need to configure RevenueCat. This involves:
- Creating a product in RevenueCat that matches the configuration in App Store Connect
- Configuring an offering, which is a set of products or a package presented to the user
- Configuring an entitlement, which represents access levels tied to products; full setup involves groups in App Store Connect
Adding RevenueCat to Your Project
To add RevenueCat to your project, you’ll need to:
- Use the Swift Package Manager to add the RevenueCat package
- Import RevenueCat in your files
- Configure your API key in a constants file or directly in code, and set up entitlements in the RevenueCat dashboard
Configuring the App Delegate
In the app delegate, you’ll need to:
- Set the log level for RevenueCat
- Configure the SDK with your API key and store kit
- Add a purchases delegate to update your UI based on subscription status
// Configure RevenueCatPurchases.configure(with: Configuration.Builder() .with(apiKey: Constants.apiKey) .with(storeKit2IfAvailable: true) .build())As you work on implementing in-app subscriptions, you may also want to explore other ways to build and automate your iOS apps, such as using Claude Code to streamline your development process.
Getting Offerings and Products
To get offerings and products, you’ll need to:
- Use the
getOfferingsmethod from RevenueCat - Handle errors and update your UI accordingly
// Get offeringsPurchases.shared.getOfferings { (offerings, error) in if let error = error { print(error.localizedDescription) } else if let offerings = offerings { // Update your UI with the offerings }}🔗 See Also: How to Build AI Systems That Actually Run Your Business (Not Just Chat)
Handling Purchases
To handle purchases, you’ll need to:
- Use the
purchasemethod from RevenueCat, preferably with a product from an offering - Handle errors and update your UI accordingly
// Handle purchasesPurchases.shared.purchase(product: product) { (transaction, error) in if let error = error { print(error.localizedDescription) } else if let transaction = transaction { // Update your UI with the transaction }}Building on the concepts of AI-powered business systems, you can also learn from How I Get AI To Follow My Designs (In-Depth Walkthrough) to improve your app’s overall user experience.
Restoring Purchases
To restore purchases, you’ll need to:
- Use the
restorePurchasesmethod from RevenueCat - Handle errors and update your UI accordingly
// Restore purchasesPurchases.shared.restorePurchases { (customerInfo, error) in if let error = error { print(error.localizedDescription) } else if let customerInfo = customerInfo { // Update your UI with the customer info }}User Identifier and Subscription Status
To get the user identifier and subscription status, you’ll need to:
- Use the
getCustomerInfomethod from RevenueCat - Handle errors and update your UI accordingly
// Get customer infoPurchases.shared.getCustomerInfo { (customerInfo, error) in if let error = error { print(error.localizedDescription) } else if let customerInfo = customerInfo { // Update your UI with the customer info }}Summary
Implementing in-app subscriptions with RevenueCat involves configuring your app, adding RevenueCat to your project, configuring the app delegate, getting offerings and products, handling purchases, restoring purchases, and getting the user identifier and subscription status. By following these steps, you can easily add in-app subscriptions to your Swift app using RevenueCat.
For a full and accurate configuration example, consider the following initialization code:
init() { Purchases.configure(withAPIKey: "api_key")}Or with a builder for more options:
Purchases.configure(with: Configuration.Builder() .with(apiKey: Constants.apiKey) .with(storeKit2IfAvailable: true) .build())