Skip to main content
Solved

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

  • December 20, 2024
  • 1 reply
  • 83 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.

View original
Did this post help you find an answer to your question?
This post has been closed for comments

1 reply

Forum|alt.badge.img+2
  • Author
  • Helper
  • 1 reply
  • 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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings