NitroUI SDK
Install @voidhash/nitro-ui in a NitroUI app, link its native code and create the client.
@voidhash/nitro-ui adds paywalls, purchases, perks, feature flags and analytics to
NitroUI apps on iOS and Android. It has the same client, provider, hooks and
types as the React Native SDK and runs on the same native iOS and Android SDKs,
so everything from creating the client onwards works as the React Native pages describe. Import from
@voidhash/nitro-ui wherever they import from @voidhash/react-native. This page covers what is
different.
NitroUI is in alpha, and so is this SDK: its API can change between versions. This release supports NitroUI 0.2.1. Update the SDK when you update NitroUI, and read the changelog before you upgrade.
Install
Add the SDK and Effect 4 with Bun, then prepare the app so NitroUI links the SDK's native code into
your ios/ and android/ projects:
bun add @voidhash/nitro-ui effect@4
bun run nitro-ui prepareThen rebuild the app on both platforms. An app created with create-nitro-ui also prepares itself
after every install, and the NitroUI CLI's ios and android commands prepare before they build;
run prepare yourself when you build from Xcode or Android Studio.
The package runs on the native iOS and Android SDKs of the same version, which the native build
installs: on iOS with Swift Package Manager, and on Android from Maven Central. Updating
@voidhash/nitro-ui updates both.
@voidhash/nitro-ui isn't on npm yet. Until its first release, install the newest
preview build:
bun add @voidhash/nitro-ui@https://pkg.voidha.sh/@voidhash/nitro-ui/branch:main effect@4.
Compatibility
| Requirement | Version |
|---|---|
| NitroUI | 0.2.1 (nitro-ui-core 0.2.1 or a later 0.2 release) |
| Nitro | nitro-core 1.0.0-alpha.nightly.137.gd4793c40b8b8, exactly |
| iOS and Android | iOS 16 and Android API 26, NitroUI's own minimums |
effect | 4.0.7 or later 4.x (peer dependency) |
react, nitro-ui-core and nitro-core are peer dependencies, which a NitroUI app already has.
NitroUI builds every native module in the app against one Nitro version, so the app and the SDK must
resolve the same nitro-core.
The package ships native code. Changing its version needs a new native build. A JavaScript bundle
running on a binary built before you added the SDK still loads, but init() fails with
FAILED_TO_INITIALIZE_VOIDHASH_CLIENT until you rebuild.
Paywalls
Paywalls are in preview: they work and you can ship them, but they may still change, and you use them at your own risk.
That is the status of paywalls on every SDK, apart from the Alpha above, which is about this SDK and NitroUI.
presentFlow() and usePresentFlow() present the paywall a location shows and resolve when it
ends, exactly as on React Native: follow Display a paywall
from Present it on, importing from @voidhash/nitro-ui. Skip its first step, adding the
renderer: a NitroUI app has it already.
Paywall renderer
The native renderer that presents paywalls is part of the SDK, so there is nothing to add or turn
on: presentFlow() works once the SDK is installed. The native build installs it with the rest of
the SDK: VoidhashUI from Swift Package Manager on iOS, and com.voidhash:voidhash-ui from Maven
Central on Android. It draws paywalls with each platform's own views.
It adds about 3 MB to your app's download on iOS, and about 2.5 MB per CPU architecture on Android. It ships for every architecture NitroUI builds for, simulators and emulators included. If you shrink your Android release build with R8, the SDK brings the rules it needs.
Ship a paywall with the app
To show a paywall on a first launch without a connection, ship what a location shows in the app.
The Voidhash CLI writes the location's answer and build,
as onboarding.json and onboarding.vhfp, into the folder each platform's SDK reads:
voidhash location bundle onboarding --out ios/VoidhashFlows
voidhash location bundle onboarding --out android/app/src/main/assets/voidhash-flowsIf the client's flowRevisions names a revision for the location, pass the same one, such as
--revision onboarding=3. Without it the command takes the answer for revision 0, and the SDK
skips files taken for another revision than the one the app presents.
- iOS: the SDK reads a folder named
VoidhashFlowsin the app. In Xcode, choose File → Add Files to "NitroUI"…, selectios/VoidhashFlows, and add it to the app target as a folder reference, so its files keep their folder. Keep it outsideios/NitroUI/, whose files Xcode adds to the app on its own. Writing new files into the folder later needs no change in Xcode. - Android: the SDK reads
voidhash-flowsin the app's assets,android/app/src/main/assets/, which the app build includes as it is.
Run the commands in your release build step, so each release ships fresh files. The
Swift and
Kotlin guides say when a shipped paywall is shown,
and why its notAfter matters.
Create the client
Create one client at module scope with your publishable keys and keep it for the life of the app:
import { createVoidhashClient } from "@voidhash/nitro-ui";
export const voidhash = createVoidhashClient(__DEV__ ? "vh_pk_test_..." : "vh_pk_...", {
dev: true,
});Release builds use the production key; debug builds use the test key, and dev: true sends their
purchases through a simulated store. See API keys and
Development mode. The options are those of the React Native
SDK: see Client options.
Then mount <voidhash.Provider> once at the root of your app. It starts the client and gives hooks
such as voidhash.useHasPerk their data:
import { voidhash } from "./lib/voidhash";
import { Home } from "./Home";
export function App() {
return (
<voidhash.Provider>
<Home />
</voidhash.Provider>
);
}Generated slug types
voidhash types generate writes voidhash.gen.d.ts, which types your product, perk and location
slugs and feature flag keys, as for React Native. In an
app that depends on @voidhash/nitro-ui, its declarations augment that package. The Metro plugin
that regenerates the file while React Native's dev server runs doesn't apply to NitroUI; run
types generate after you change slugs in Studio.
Track screens
The SDK records a $screen event each time a new screen appears, with the screen the user came from
and how long they stayed there, as on React Native. In an app
that navigates with nitro-ui-navigation, pass the handlers from useScreenTracking to its
NavigationContainer:
import { NavigationContainer } from "nitro-ui-navigation";
import { useScreenTracking } from "@voidhash/nitro-ui/navigation";
import { voidhash } from "./lib/voidhash";
import { RootNavigator } from "./RootNavigator";
export function App() {
return (
<voidhash.Provider>
<AppNavigation />
</voidhash.Provider>
);
}
function AppNavigation() {
const screenTracking = useScreenTracking();
return (
<NavigationContainer
ref={screenTracking.ref}
onReady={screenTracking.onReady}
onStateChange={screenTracking.onStateChange}
>
<RootNavigator />
</NavigationContainer>
);
}Call useScreenTracking in a component rendered inside <voidhash.Provider>. Screens the user
reaches with the native back button, back gesture, sheet dismissal or tab bar are recorded like
the ones your code navigates to. The screen name is the focused route's name and the path is the
chain of navigators, such as /Tabs/Home/Feed; $screen_source is nitro-ui-navigation. A
screen whose params change is still the same screen, while the same route opened again, such as a
second Details screen, is recorded again. If you already have a container ref, pass it with
useScreenTracking({ ref }) and call onReady and onStateChange from your own callbacks.
To stop storing $screen for a project without a release, turn it off in Settings → Events,
or with voidhash events policy disable '$screen'. See Events.
@voidhash/nitro-ui/navigation needs nitro-ui-navigation 0.2.1 or a later 0.2 release and the
@react-navigation/core it installs; apps that don't use it don't need either. For screens no
navigator shows, such as onboarding pager pages, call
screen(). The screenTracking client
option turns tracking off, adds route params or renames screens for both; see
Options.
Differences from React Native
The NitroUI SDK leaves out what is tied to React Native's tooling:
- Expo Router and React Navigation adapters.
@voidhash/react-native/expo-routerand@voidhash/react-native/react-navigationare for React Native's routers; NitroUI apps use@voidhash/nitro-ui/navigationinstead. - Metro plugin.
@voidhash/react-native/metroisn't included; see Generated slug types. - Expo config plugin. NitroUI apps keep their native projects in the repository, and
nitro-ui preparelinks the SDK, so there is no plugin to register.
Next steps
- See a complete sample app: products, restore, a paywall with a fallback screen, sign-in, a feature flag and screen tracking.
The React Native pages apply to this SDK as they are:
- Quickstart: sell a product and unlock a perk, from Show products and buy on
- Display a paywall, without its renderer step (see Paywalls)
- Make and restore purchases
- Check entitlements
- Identify users
- Feature flags and analytics
- Reference, errors and troubleshooting
- Data collection, for the App Store privacy details and the Google Play Data safety form
Something wrong or missing on this page? Tell us by email or on Discord.