Manually Handling Purchases and Subscription Status
If you need fine-grain control over the purchasing pipeline, use a purchase controller.
Using a PurchaseController is only recommended for advanced use cases. Superwall handles all
subscription-related logic and purchasing operations for you out of the box.
By default, Superwall handles basic subscription-related logic for you:
Purchasing: When the user initiates a checkout on a paywall.
Restoring: When the user restores previously purchased products.
Subscription Status: When the user’s subscription status changes to active or expired (by checking the local receipt).
However, if you want more control, you can pass in a PurchaseController when configuring the SDK via configure(apiKey:purchaseController:options:) and manually set Superwall.shared.subscriptionStatus to take over this responsibility.
A PurchaseController handles purchasing and restoring via protocol methods that you implement. You pass in your purchase controller when configuring the SDK:
Copy
Ask AI
// MyPurchaseController.swiftimport SuperwallKitimport StoreKitfinal class MyPurchaseController: PurchaseController { static let shared = MyPurchaseController() // 1 func purchase(product: StoreProduct) async -> PurchaseResult { // Use StoreKit or some other SDK to purchase... // Send Superwall the result. return .purchased // .cancelled, .pending, .failed(Error) } func restorePurchases() async -> RestorationResult { // Use StoreKit or some other SDK to restore... // Send Superwall the result. return .restored // Or failed(error) }}
Copy
Ask AI
import StoreKitimport SuperwallKitfinal class SWPurchaseController: PurchaseController { // MARK: Sync Subscription Status /// Makes sure that Superwall knows the customer's subscription status by /// changing `Superwall.shared.subscriptionStatus` func syncSubscriptionStatus() async { var products: Set<String> = [] for await verificationResult in Transaction.currentEntitlements { switch verificationResult { case .verified(let transaction): products.insert(transaction.productID) case .unverified: break } } let storeProducts = await Superwall.shared.products(for: products) let entitlements = Set(storeProducts.flatMap { $0.entitlements }) await MainActor.run { Superwall.shared.subscriptionStatus = .active(entitlements) } } // MARK: Handle Purchases /// Makes a purchase with Superwall and returns its result after syncing subscription status. This gets called when /// someone tries to purchase a product on one of your paywalls. func purchase(product: StoreProduct) async -> PurchaseResult { let result = await Superwall.shared.purchase(product) await syncSubscriptionStatus() return result } // MARK: Handle Restores /// Makes a restore with Superwall and returns its result after syncing subscription status. /// This gets called when someone tries to restore purchases on one of your paywalls. func restorePurchases() async -> RestorationResult { let result = await Superwall.shared.restorePurchases() await syncSubscriptionStatus() return result }}
Here’s what each method is responsible for:
Purchasing a given product. In here, enter your code that you use to purchase a product. Then, return the result of the purchase as a PurchaseResult. For Flutter, this is separated into purchasing from the App Store and Google Play. This is an enum that contains the following cases, all of which must be handled:
.cancelled: The purchase was cancelled.
.purchased: The product was purchased.
.pending: The purchase is pending/deferred and requires action from the developer.
.failed(Error): The purchase failed for a reason other than the user cancelling or the payment pending.
Restoring purchases. Here, you restore purchases and return a RestorationResult indicating whether the restoration was successful or not. If it was, return .restore, or failed along with the error reason.
You must set Superwall.shared.subscriptionStatus every time the user’s subscription status changes, otherwise the SDK won’t know who to show a paywall to. This is an enum that has three possible cases:
.unknown: This is the default value. In this state, paywalls will not show and their presentation will be automatically delayed until subscriptionStatus changes to a different value.
.active(let entitlements): Indicates that the user has an active entitlement. Paywalls will not show in this state unless you remotely set the paywall to ignore subscription status. A user can have one or more active entitlement.
.inactive: Indicates that the user doesn’t have an active entitlement. Paywalls can show in this state.
Here’s how you might do this:
Copy
Ask AI
import SuperwallKitfunc syncSubscriptionStatus() async { var purchasedProductIds: Set<String> = [] // get all purchased product ids for await verificationResult in Transaction.currentEntitlements { switch verificationResult { case .verified(let transaction): purchasedProductIds.insert(transaction.productID) case .unverified: break } } // get store products for purchased product ids from Superwall let storeProducts = await Superwall.shared.products(for: purchasedProductIds) // get entitlements from purchased store products let entitlements = Set(storeProducts.flatMap { $0.entitlements }) // set subscription status await MainActor.run { Superwall.shared.subscriptionStatus = .active(entitlements) }}
If you need a simple way to observe when a user’s subscription status changes, on iOS you can use the Publisher for it. Here’s an example:
Copy
Ask AI
subscribedCancellable = Superwall.shared.$subscriptionStatus .receive(on: DispatchQueue.main) .sink { [weak self] status in switch status { case .unknown: self?.subscriptionLabel.text = "Loading subscription status." case .active(let entitlements): self?.subscriptionLabel.text = "You currently have an active subscription: \(entitlements.map { $0.id }). Therefore, the paywall will not show unless feature gating is disabled." case .inactive: self?.subscriptionLabel.text = "You do not have an active subscription so the paywall will show when clicking the button." } }