Display a paywall
Present the paywall or onboarding flow shown at a location and handle how it ended.
The SDK presents the paywall or onboarding flow that Studio shows at a paywall location, drawn natively on iOS and Android. You change what appears at a location in Studio, without an app release. See Paywalls in your app.
Paywalls are in preview: they work and you can ship them, but they may still change, and you use them at your own risk.
Add the paywall renderer
Paywalls are drawn by a native renderer that your app includes only when you turn it on, so apps
that don't show paywalls don't ship it. Without it, presenting fails with FLOW_RENDERER_MISSING
and the SDK makes no paywall requests.
In an Expo app, set the config plugin's renderer option, then run npx expo prebuild and rebuild:
{
"expo": {
"plugins": [["@voidhash/react-native", { "renderer": true }]]
}
}In a bare app, set the Podfile global inside your app target, above use_native_modules!, and the
Gradle property, then run npx pod-install and rebuild:
$VoidhashRenderer = trueVoidhash_renderer=trueThe renderer installs with the native SDKs: VoidhashUI from Swift Package Manager on iOS, and
com.voidhash:voidhash-ui from Maven Central on Android. On iOS it runs on devices and in the
Simulator on Apple silicon Macs.
Present it
presentFlow(location) and usePresentFlow(location) take a location slug, such as
onboarding: the spot in your app where a flow is shown. They present whatever is shown there and
resolve when it ends. They reject when nothing could be shown.
import { Button } from "react-native";
import { useRouter } from "expo-router";
import { voidhash } from "../src/lib/voidhash";
export function UpgradeButton() {
const router = useRouter();
const { present, isPresenting } = voidhash.usePresentFlow("onboarding");
return (
<Button
disabled={isPresenting}
title="See plans"
onPress={async () => {
try {
await present();
} catch {
router.push("/plans");
}
}}
/>
);
}Outside React, call the client. A flow already on screen is closed first and resolves as
dismissed.
dismissFlow() also cancels a flow that is still loading, so it doesn't appear later.
const result = await voidhash.client.presentFlow("onboarding");
await voidhash.client.dismissFlow(); // closes it; the pending call resolves as dismissedHandle the result
Every ending has the same shape: { outcome, output, productId? }.
outcome | Meaning |
|---|---|
purchased | A purchase ended the flow. productId is the store product bought. |
restored | A restore ended the flow. |
dismissed | The user closed it, you called dismissFlow(), or another flow replaced it. |
| anything else | The flow ran a Finish flow action; the outcome is that action's result, such as completed. |
output holds the flow's outputs by name, such as survey answers, whatever the outcome. It is empty
for a flow without outputs.
A flow that could not be shown is not an outcome: the call rejects, see Fall back when nothing was shown.
An onboarding flow that doesn't end on a purchase or restore continues to its next screen and ends
with one of its own outcomes or dismissed, so check the perk after it ends. A failed or cancelled
purchase leaves any flow open, so the user can try again.
A paywall with an On close action in Studio runs it the first time the user tries to close,
for example to show a last offer, and presentFlow keeps waiting. dismissFlow() always closes at
once.
Purchases from a paywall go through the SDK's purchase path. In observer mode
they fail with READ_ONLY_PURCHASE_NOT_ALLOWED, and the paywall shows the failure.
Pass inputs and handle app actions
A flow can take inputs from your app, report outputs when it ends, and call app actions your app handles while it is on screen. Values are strings, numbers, booleans and arrays of strings (lists, each string once, at most 100 of at most 200 characters).
const result = await voidhash.client.presentFlow("onboarding", {
input: { firstName: user.name, credits: 3 },
actions: {
pickGoal: async ({ current }) => ({ goal: await openGoalPicker(current) }),
},
});
if (result.outcome === "completed") saveGoal(result.output.goal);inputsets the flow's inputs by name. An input you leave out keeps the default set in Studio.actionshas one handler per app action. A handler receives the action's arguments and returns its reply, or a promise of it. When a handler throws or rejects, is missing, or takes longer than the action allows, the flow runs the failure path designed for that action, so the user is never left waiting. In a debug build the SDK logs an action the flow called without a handler.- To change inputs while the flow is on screen, call
updateFlowInput, orupdateInputfrom the hook. Inputs you don't name keep their values, and the call does nothing when no flow is shown.
await voidhash.client.updateFlowInput({ credits: 2 });
const { present, updateInput } = voidhash.usePresentFlow("onboarding");Typed locations
The declarations voidhash-cli types generate writes describe each location's inputs, outputs,
outcomes and app actions. With them in your project, presentFlow("onboarding", …) checks the
inputs and handlers at compile time and types result.outcome and result.output; the options are
required when the flow has required inputs or app actions. purchased, restored and dismissed
are part of every location's outcomes. A location without declarations takes and returns plain
dictionaries.
The generated code also exports flowRevisions: the revision of each location's interface your app
was built against. Pass it once when you create the client:
import { createVoidhashClient } from "@voidhash/react-native";
import { flowRevisions } from "../../voidhash.flows.gen";
export const voidhash = createVoidhashClient("vh_pk_...", { flowRevisions });Voidhash then serves each app version a flow that fits the interface it was built against. The
revision is used for preloading and for every presentFlow call; to override it for one call, pass
revision in its options. A location that isn't listed counts as revision 0.
Fall back when nothing was shown
presentFlow and present reject with an error whose code says why nothing was shown. Keep your
own upgrade screen for it, so important entry points always offer a way to upgrade.
try {
await voidhash.client.presentFlow("upgrade");
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "FLOW_UNAVAILABLE") retryLater();
router.push("/plans");
}| Code | Cause | Fix |
|---|---|---|
FLOW_NOT_ASSIGNED | Nothing is shown at the location, the location is missing or archived, or none of its recent builds is one this app can show. | Show a paywall at the location; meanwhile show your own screen. |
FLOW_UNAVAILABLE | The device has never loaded this location and can't reach Voidhash, or the paywall wasn't ready to show within 15 seconds. | Retry later. |
FLOW_RENDERER_MISSING | The build doesn't include the renderer. | Add the paywall renderer. |
FLOW_UNSUPPORTED_PACKAGE | The location's build uses a package format this app's SDK build can't read, or on Android the renderer's native library doesn't ship for the device's CPU. | Show your own screen; report the error. |
FLOW_PRESENTATION_FAILED | There was no screen to present from, or the paywall couldn't be drawn. | Present from a mounted screen; report the error. |
FLOW_DISABLED | The client was created with enabled: false. | — |
To check what a location shows without presenting it, call getPaywallForLocation(slug). It
resolves to Ok(null) when nothing is shown there. Like presenting, it counts the person in the
A/B test that chose the paywall, even when you only look at what is assigned, and records
$paywall_handed_off on each call that returns such a paywall.
Caching and preloading
The device caches what each location shows for up to 7 days, so a location shown before works offline, and a new deploy reaches devices within about 5 minutes of their next presentation or return to the foreground. Paywalls in your app explains when it asks Voidhash again.
presentFlow gives a paywall 15 seconds from the call to be ready to show. One that isn't ready by
then is never shown: the call rejects with FLOW_UNAVAILABLE, and the download
finishes in the background for the next presentation. A paywall on screen has no time limit.
Apps show a loading screen while a paywall loads from the network: the native SDK shows it 0.4
seconds after the call, at least long enough to be read, until the paywall takes its place; a
paywall already on the device appears before then, without it. The end user can close it, and
presentFlow then resolves with the outcome dismissed and wasShown: false, which tells it apart
from a paywall the end user closed. To keep your own screen until the paywall is ready, turn it off:
import { createVoidhashClient } from "@voidhash/react-native";
export const voidhash = createVoidhashClient("vh_pk_...", { flowLoading: "none" });To show a paywall on a first launch without a connection, ship it in the native projects. The
Voidhash CLI writes the location's answer and build, as
onboarding.json and onboarding.vhfp, into the folder each SDK reads: ios/<App>/VoidhashFlows/,
a folder reference of the app target, and android/app/src/main/assets/voidhash-flows/.
voidhash location bundle onboarding --out ios/MyApp/VoidhashFlows
voidhash location bundle onboarding --out android/app/src/main/assets/voidhash-flowsThe Swift and
Kotlin guides say what the answer is, when it is
shown, how to make the files without the CLI, and why its notAfter matters.
A paywall's images are kept on the device too, up to 64 MB, so a cached paywall shows them while
their host can't be reached. The SDK fetches them ahead with the paywall, except on a metered
connection such as cellular data. Only images of up to 10 MB from https addresses on the
internet are loaded, never from the device itself or its local network, nor from an address with a
user name or password, and a redirect is followed only to such an address. An embedded data:
image shows when it is at most 10 MB. A debug build also loads images from the development server
it is configured with. Your Voidhash server, the one baseUrl names, is always allowed over https
whatever its address, and so are images uploaded to your project. An image uploaded to your project
shows only when its bytes are the ones recorded when it was uploaded: one that differs fails like
any other image, isn't kept, and is reported once per image and build each time the app runs as
$paywall_image_integrity_failed. Only PNG, JPEG, GIF and WebP images are kept. A kept image isn't
downloaded again, so publish changed artwork under a new URL.
Locations the device has shown before download again at launch. List new ones in
preloadLocations to download them at launch too:
import { createVoidhashClient } from "@voidhash/react-native";
export const voidhash = createVoidhashClient("vh_pk_...", {
preloadLocations: ["onboarding"],
});Choose what paywalls may do
Purchases, restores, closing, navigation and analytics always work. Other actions need a capability
granted with flowCapabilities; by default only links is granted.
import { createVoidhashClient } from "@voidhash/react-native";
export const voidhash = createVoidhashClient("vh_pk_...", {
flowCapabilities: ["links", "notifications", "attributes"],
});| Capability | Allows the Studio action | Platform requirement |
|---|---|---|
links | Open URL | — |
notifications | Request permission for notifications | Android 13+: declare android.permission.POST_NOTIFICATIONS |
tracking | Request permission for tracking | iOS: a native handler (below). Android: unavailable. |
attributes | Set user attribute | — |
An action whose capability isn't granted is skipped with a warning, and a Request permission
step for it goes on at its Unavailable exit, as it does where the platform requirement is
missing. In Expo, declare the requirements under android.permissions and ios.infoPlist in
app.json.
Permission handlers
The SDK asks for notifications itself. It has no tracking prompt of its own, and doesn't link or
reference AppTrackingTransparency. To let a flow ask for tracking, or to ask for notifications in
your own way, register a handler in native code: JavaScript can't supply one. Register it before
the JavaScript client starts, and add the AppTrackingTransparency framework and
NSUserTrackingUsageDescription to your app for tracking.
import AppTrackingTransparency
import NitroVoidhash
import Voidhash
// In application(_:didFinishLaunchingWithOptions:)
VoidhashFlowPermissionHandlers.register(
FlowCapability.tracking.rawValue,
FlowPermissionHandler(
status: {
switch ATTrackingManager.trackingAuthorizationStatus {
case .authorized: return .granted
case .denied, .restricted: return .denied
case .notDetermined: return .notDetermined
@unknown default: return .unavailable
}
},
request: {
// Asked before the app is active, the system shows no prompt.
let active = await MainActor.run { UIApplication.shared.applicationState == .active }
if !active {
for await _ in NotificationCenter.default.notifications(
named: UIApplication.didBecomeActiveNotification) { break }
}
switch await ATTrackingManager.requestTrackingAuthorization() {
case .authorized: return .granted
case .denied, .restricted: return .denied
default: return .unavailable
}
}))On Android, register a FlowPermissionHandler in Application.onCreate with
VoidhashFlowPermissionHandlers.register(FlowCapability.NOTIFICATIONS.wireName, handler) from
com.margelo.nitro.voidhash; the Kotlin paywall guide shows a handler.
Handlers run off the main thread, status on every configure and whenever the app returns, and
request must return once the person answered; see the Swift and Kotlin guides for the details.
On Android a status is a best guess: Android can't tell a permission never asked for from one the
person denied for good, as the Kotlin guide explains.
A permission's status stays on the device unless the flow puts it into a tracked event's property
or a person attribute.
Saved progress
A flow saves its variables on the device for each person and flow. Presenting the same flow again
restores the variables, not the screen. A Finish flow action clears them, and so do identify()
and reset(). A purchase inside an onboarding flow doesn't clear them.
Analytics
Each presentation records what the native SDKs record: exactly one of $paywall_viewed,
$paywall_load_failed or $paywall_load_cancelled, and for a viewed paywall then
$paywall_completed or $paywall_dismissed. Every event about a paywall carries packageSource
once its build is loaded: network, cache or bundled (shipped in the app). The
Swift and Kotlin
guides list each event's properties.
Events a paywall records carry paywall_location, paywallId, paywallLocationId,
paywallShowingId and paywallReleaseId, and each screen is recorded as a $screen event. A
purchase made on a paywall is credited, with its renewals, to the location, paywall, build and A/B
test variant. Purchases made outside a paywall, and restores, aren't credited to one.
Next steps
Something wrong or missing on this page? Tell us by email or on Discord.