InAppPurchaseKit Module Overview
Powered by RevenueCat (Official Partner of SwiftyLaunch)
InAppPurchaseKit wraps RevenueCat's SDK to allow for an even easier integration of paywalls and in-app purchases in your app.
After generating a project with InAppPurchaseKit enabled and completing the initial project setup, you will get following features out of the box:
- A customizable pre-built paywall: A paywall view that allows you to easily customize it to your liking. Read more.
- Ability to lock actions behind a paywall: Lock features behind a paywall and show the paywall when the user tries to access them. Read more.
- Ability to lock views behind a paywall: Lock views behind a paywall and show the paywall when the user tries to access them. Read more.
- An entry in the app settings screen: A dedicated view in the app settings screen to manage subscriptions and purchases. Read more.
- (AnalyticsKit only) Automatic analytics tracking: Automatically track events related to paywalls and purchases. Read more.
- (AuthKit only) Ability to use a shared user ID: If the user is signed in, the Firebase user ID is used as the alias ID for RevenueCat. Read more.
- (BackendKit only) BackendKit integration: You can also access InAppPurchaseKit information on the backend. Read more.
Module Deep Dive
Initialization and Configuration
- We initilize InAppPurchaseKit in
App.swift
: by calling a static functionInAppPurchases.initRevenueCat()
- Only when generated with AuthKit: We observe if the user has signed into an account, and use the Firebase user ID as the alias ID for RevenueCat, so the user
can be accurately tracked across different providers via
InAppPurchases.associateUserWithID(USER_ID)
. We remove that alias when the user signes out by callingInAppPurchases.removeUserIDAssociation()
.
General Usage
The InAppPurchases
object was designed to be used as an EnvironmentObject throughout the application and is attached to the root of the view hierarchy in App.swift
. By default,
we use the abbreviation iap
for the environment object.
Here are some of the relevant InAppPurchases object parameters and functions that you can access:
Access Subscription State
@Published public private(set) var subscriptionState: SubscriptionState = .notSubscribed
Can either be .notSubscribed
or .subscribed
. Updated automatically by the listenForSubscriptionStateChange()
listener that is called during initialization.
Show Paywall Sheet
static public func showPaywallSheet() { }
A static InAppPurchases function that will show the paywall sheet. The function sends a notification to the notification center while the view
modifier placed in App.swift
called ShowPaywallSheetWhenCalledModifier()
listens to that notification and presents the paywall sheet.
Execute if User is Subscribed
public func executeIfGotPremium(otherwise consequence: NoPremiumConsequence = .showPaywall, _ closure: () -> Void) { }
A function that that executes the provided closure if the user is subscribed. If the user is not subscribed, the consequence
parameter will determine what happens next. (Will either show the paywall or show an in-app notification saying that the feature required premium).
Read more in the Lock Actions behind Paywall section.
Show System Subscription Management Screen
static public func showSubscriptionManagementScreen() async {}
A static InAppPurchases function that opens the system subscription management screen. Is shown on the InAppPurchases Settings Screen (when the user taps on "Manage Subscription").
Show View if User is Subscribed
public func requirePremium(iap: InAppPurchases, onCancel: @escaping () -> Void) -> some View {}
A view modifier that allows you to lock views behind the paywall. If the user is subscribed, the view will be shown as usual, otherwise the paywall will be shown. Read more in the Lock Views behind Paywall section.
AuthKit Integration
If AuthKit is selected during project generation, your app will automatically add Firebase User ID as an alias to the RevenueCat user ID when the user signes in. This allows you to access user information on the dashboard or on the backend (using BackendKit) by simply using the Firebase User ID.
AnalyticsKit Integration
If AnalyticsKit is selected during project generation, most of the events related to paywalls and purchases are automatically tracked with iap
as the source.
An example entry in the PostHog dashboard:
Module File Contents
Let's go through every file that is contained in the InAppPurchaseKit module:
Config
Contains the RevenueCat-Info.plist
file that you will paste your RevenueCat API key into.
Resources
Contains the hero video placeholder that is shown by default on the first page in the carousel of the paywall
Sources
Model / InAppPurchases.swift
Contains the InAppPurchases
observable class, which is the entry point to interact with the module.
ViewModifier / requirePremium.swift
Exposes the .requirePremium()
view modifier that allows you to lock views behind the paywall. See
Lock Views behind Paywall for more information.
ViewModifier / ShowPaywallSheetWhenCalledModifier.swift
Exposes the ShowPaywallSheetWhenCalledModifier()
view modifier that listens for the showPaywallSheet()
notification and presents the paywall sheet.
Because we only use it in the root view in App.swift
, we don't wrap it around a more general view modifier, and attach it via .modifier(ShowPaywallSheetWhenCalledModifier())
.
Additionally, extends the InAppPurchases
class with the showPaywallSheet()
function that sends the notification.
View / InAppPurchaseView.swift
The paywall view that is shown when the user tries to access a feature that requires premium. You can customize this view to your liking, see Paywall for more information.
View / PremiumSettingsView.swift
View that can contains premium-related settings in the app. By default shows the "Manage Subscription" button that opens the system subscription management screen. See Premium Settings Screen for more information.