Create a new file called RCPurchaseController.swift or RCPurchaseController.kt, then copy and paste the following:
importSuperwallKitimportRevenueCatimportStoreKitfinalclassRCPurchaseController:PurchaseController{// MARK: Sync Subscription Status/// Makes sure that Superwall knows the customers subscription status by/// changing `Superwall.shared.subscriptionStatus`funcsyncSubscriptionStatus(){assert(Purchases.isConfigured,"You must configure RevenueCat before calling this method.")Task{forawait customerInfo inPurchases.shared.customerInfoStream {// Gets called whenever new CustomerInfo is availablelet hasActiveSubscription =!customerInfo.entitlements.active.isEmpty // Why? -> https://www.revenuecat.com/docs/entitlements#entitlementsif hasActiveSubscription {Superwall.shared.subscriptionStatus =.active
}else{Superwall.shared.subscriptionStatus =.inactive
}}}}// MARK: Handle Purchases/// Makes a purchase with RevenueCat and returns its result. This gets called when/// someone tries to purchase a product on one of your paywalls.funcpurchase(product:SKProduct)async->PurchaseResult{do{// This must be initialized before initiating the purchase.let purchaseDate =Date()let storeProduct =RevenueCat.StoreProduct(sk1Product: product)let revenueCatResult =tryawaitPurchases.shared.purchase(product: storeProduct)if revenueCatResult.userCancelled {return.cancelled
}else{iflet transaction = revenueCatResult.transaction,
purchaseDate > transaction.purchaseDate {return.restored
}else{return.purchased
}}}catchlet error asErrorCode{if error ==.paymentPendingError {return.pending
}else{return.failed(error)}}catch{return.failed(error)}}// MARK: Handle Restores/// Makes a restore with RevenueCat and returns `.restored`, unless an error is thrown./// This gets called when someone tries to restore purchases on one of your paywalls.funcrestorePurchases()async->RestorationResult{do{_=tryawaitPurchases.shared.restorePurchases()return.restored
}catchlet error {return.failed(error)}}}
As discussed in Purchases and Subscription Status, this PurchaseController is responsible for handling the subscription-related logic. Take a few moments to look through the code to understand how it does this.