Features
In-App Purchases
In-app purchases are essential for monetizing your application. ShipThatApp
utilizes RevenueCat to simplify and manage the in-app purchase process. The boilerplate is pre-configured with RevenueCat
for handling subscriptions and consumable products.
PurchaseManager
PurchaseManager.swift
is central to the in-app purchase functionality. This class handles fetching product information, purchasing products, and managing transaction states.
Key Components of PurchaseManager
Properties
productIds
: A set of strings that are the identifiers for your products. These should match the product IDs set up in App Store Connect and RevenueCat.products
: An array ofProduct
objects that represent the products available for purchase.purchasedProductIDs
: A set that keeps track of the product IDs that have been purchased by the user.
Functions
loadProducts()
: CallsProduct.products(for:)
to load theSKProduct
information from the App Store.purchase(_ product: Product)
: Initiates a purchase transaction for the givenProduct
.updatePurchasedProducts()
: Checks the current entitlements to updatepurchasedProductIDs
accordingly.
Transaction Observer
observeTransactionUpdates
: Observes the StoreKit transaction queue and processes updates accordingly.
RevenueCat Configuration
To configure RevenueCat, you need an API key from the RevenueCat dashboard. This key needs to be added to your Info.plist
file.
Steps
Get the RevenueCat API Key from your RevenueCat project.
Add the key to your
Info.plist
file, for example:<key>RC_API_KEY</key> <string>Your_RevenueCat_API_Key</string>
Initialize RevenueCat in
configureRevenueCat()
in the app entry point (ShipThatAppApp.swift
).
Handling Transactions
PurchaseManager
listens for transaction updates by subscribing to Transaction.updates
. Each transaction is processed to determine whether it is verified or unverified, pending, or user-cancelled.
Successful Transactions
- Upon a successful purchase,
transaction.finish()
is called if necessary, andupdatePurchasedProducts()
updates the app state to reflect the purchase's success.
Restoring Purchases
- The user can restore purchases which will sync previously completed transactions—this is also managed by the
PurchaseManager
.
Paywall Views
The application includes views dedicated to displaying in-app purchase options to the user.
Paywall1View
- Uses StoreKit to present pre-built
ProductView
components that display product information and purchase options.
Paywall2View
- Employes RevenueCat's Paywall system to show more customized purchase options.
Paywall3View
- Provides a hand-crafted view to display product information. Taps into
PurchaseManager
to manage purchases when users interact with the paywall.
Code Example
struct Paywall3View: View {
@Environment(PurchaseManager.self) private var purchaseManager
var body: some View {
ForEach(purchaseManager.products, id: \.self) { product in
Button(action: {
// Initiate the purchase
await purchaseManager.purchase(product)
}) {
// Custom view representing the product
Text(product.displayName)
}
}
}
}
Customizing Paywalls
Developers can create custom paywall interfaces based on the Product
information fetched by the PurchaseManager
. The product object includes localized pricing, description, and title, which can be used to build detailed paywalls with buttons that trigger purchases or restore operations.