Skip to main content

So I’m currently trying to implement revenuecat into a swiftui app. This is my first foray into swiftui and I haven’t really done any swift in years either so the lifecycle events are confusing me.

I’ve successfully implemented the revenuecat SDK and in my “app” class I have a “Stream” setup to listen to events like so. My model is quite simple just those 2 properties and a computed property of isUnlocked that looks for an active entitlement.
 

AppView().environmentObject(self.revenueCatCustomerData)
.task {
// Listens to AsyncSequence for customer info updates
for await customerInfo in Purchases.shared.customerInfoStream {
self.revenueCatCustomerData.customerInfo = customerInfo
self.revenueCatCustomerData.appUserID = Purchases.shared.appUserID


}
}



I also make use of the paywall feature. However whenever I open my app, the paywall will always flash up as loading and then disappear (I assume as the revenuecat sdk is checking the entitlements). 

I’m trying to avoid the paywall flash by adding in a “purchasesLoaded” boolean in my AppView however despite setting this to false by default and only setting it to true in a task as outlined below, I still see this flash.

Is anyone able to help guide me here to prevent this?
 

struct AppView: View {
@State var isAuthenticated = false
@ObservedObject var model = DataModel.shared
@EnvironmentObject var revenueCatCustomerData: SubscriptionModel
@State private var purchasesLoaded = false

if(!isAuthenticated) {
LoginView()
}

else if(!revenueCatCustomerData.isUnlocked && isAuthenticated && purchasesLoaded) {
let _ = print("hit here")
let _ = model.reset()
PaywallView()
} else {
ContentView()
} .task {
for await state in supabase.auth.authStateChanges {
if b.initialSession, .signedIn, .signedOut].contains(state.event) {

isAuthenticated = state.session != nil
if(isAuthenticated == true) {

let userId = supabase.auth.currentSession!.user.id
let _ = try? await Purchases.shared.logIn(userId.uuidString)
let customerInfo = try? await Purchases.shared.customerInfo()
self.purchasesLoaded = true
print("Task to get customer info \(customerInfo)")
if (customerInfo?.entitlements.active != nil) {

print(customerInfo?.entitlements.active)
}else {
print("no active subscription")
}
}
}
}
}

}


Thanks

Hi ​@devcoder,

I’d recommend using our own presentIfNeeded for this case, we automatically keep track of the entitlements and show it when needed avoiding this flashing issue.

struct App: View {
var body: some View {
ContentView()
.presentPaywallIfNeeded(
requiredEntitlementIdentifier: "pro",
purchaseCompleted: { customerInfo in
print("Purchase completed: \(customerInfo.entitlements)")
},
restoreCompleted: { customerInfo in
// Paywall will be dismissed automatically if "pro" is now active.
print("Purchases restored: \(customerInfo.entitlements)")
}
)
}
}

You just need to use it like a view modifier from the parent view you want to show it from.