Skip to main content
Answer

How do I force RevenueCat to show the default paywall again after user clicks a button?

  • December 20, 2024
  • 1 reply
  • 398 views

Forum|alt.badge.img+2

Hi,

In my SwiftUI app, I manage the app state with enums, and the paywall only presents to the user when I set the value of uiState to .displayPaywall.

When this happens, the following code runs:
 

case .displayPaywall(let uuid):
PaywallView {
viewModel.retry()
}
.presentPaywallIfNeeded(
requiredEntitlementIdentifier: "premium") { _ in
viewModel.fetchLandmarkInfoIfAllowed()
}
}


And this is the PaywallView. The idea being that if the user dismisses the Paywall by clicking the X button, I show a simple message and a button that should supposedly trigger the Paywall view again:
 

struct PaywallView: View {
let onRetry: () -> Void
var body: some View {
VStack {
Text("Max free daily limit reached. Upgrade to Premium for unlimited use.")
.multilineTextAlignment(.center)
.padding()
Button("Retry") {
onRetry()
}
}
}
}


The issue I am facing now is that since RevenueCat SDK takes care of all the logic of whether Paywall should be presented or not, I can’t find a way to trigger the Paywall again with the click of a button. 

Once the retry() is fired, I just want the Paywall to come back up. By current logic, the user needs to restart the app to see the Paywall again, which is not ideal at all.

Best answer by matej-codes

Figured out a little SwiftUI “hack” to make it work.

 

ZStack {
PaywallView {
viewModel.fetchLandmarkInfoIfAllowed()
}
.presentPaywallIfNeeded(
requiredEntitlementIdentifier: "premium") { _ in
viewModel.fetchLandmarkInfoIfAllowed()
}
}
.id(uuid) // A hack to force the `presentPaywallIfNeeded` to refresh and execute again


I generate a new UUID() every time this code gets executed. That way, the view hierarchy is forced to redraw and the paywall is displayed again, even though technically the user never left the original SwiftUI view.

This post has been closed for comments

1 reply

Forum|alt.badge.img+2
  • Author
  • Helper
  • Answer
  • December 20, 2024

Figured out a little SwiftUI “hack” to make it work.

 

ZStack {
PaywallView {
viewModel.fetchLandmarkInfoIfAllowed()
}
.presentPaywallIfNeeded(
requiredEntitlementIdentifier: "premium") { _ in
viewModel.fetchLandmarkInfoIfAllowed()
}
}
.id(uuid) // A hack to force the `presentPaywallIfNeeded` to refresh and execute again


I generate a new UUID() every time this code gets executed. That way, the view hierarchy is forced to redraw and the paywall is displayed again, even though technically the user never left the original SwiftUI view.