SwiftUI
Presenting a Paywall with SwiftUI
Attach the .presentPaywall(isPresented:onPresent:onDismiss:onFail:)
view modifier to a view.
This presents a paywall when a binding to a Boolean value that you provide to isPresented
is true.
For example, here's how you might present a paywall when the user toggles the showPaywall
variable by tapping on the Toggle Paywall button:
import Paywall
import SwiftUI
struct ContentView: View {
@State private var showPaywall = false
var body: some View {
Button(
action: {
showPaywall.toggle()
},
label: {
Text("Toggle Paywall")
}
)
.presentPaywall(
isPresented: $showPaywall,
onPresent: { paywallInfo in
print("paywall info is", paywallInfo)
},
onDismiss: { result in
switch result.state {
case .closed:
print("User dismissed the paywall.")
case .purchased(productId: let productId):
print("Purchased a product with id \(productId), then dismissed.")
case .restored:
print("Restored purchases, then dismissed.")
}
},
onFail: { error in
print("did fail", error)
}
)
}
}
The onPresent
, onDismiss
, and onFail
callbacks are optional. However, they provide the following information:
Parameter | Type | Functionality |
---|---|---|
onPresent | (PaywallInfo?) → Void | A closure that’s called after the paywall is presented. Accepts a PaywallInfo? object containing information about the paywall. Defaults to nil. |
onDismiss | (PaywallDismissalResult) -> Void | The closure to execute after the paywall is dismissed by the user, by way of purchasing, restoring or manually dismissing. Accepts a PaywallDismissalResult object. This has a paywallInfo property containing information about the paywall and a state that tells you why the paywall was dismissed. This closure will not be called if you programmatically set isPresented to false to dismiss the paywall.Defaults to nil. |
onFail | (NSError) -> Void | A closure that’s called when the paywall fails to present, either because an error occured or because all paywalls are off in the Superwall Dashboard. You should typically fallback to your previous paywall if this happens. Accepts an NSError with more details. Defaults to nil. |
Presented paywalls are sticky – once a paywall is assigned to a user, they will see that paywall forever even if the paywall is turned off.
See Paywall Assignment for more information about how presented paywalls are assigned.
Updated 10 months ago