Swift SDK
Install the Voidhash iOS SDK and create its client.
The Swift SDK adds paywalls, purchases, access checks, feature flags and analytics to native iOS apps built on StoreKit 2. To add it to an app for the first time, follow the Swift quickstart. Options and methods are in the Swift SDK reference.
Requirements
- iOS 15 or later. Paywalls need UIKit.
- A Swift 6 toolchain. The sources also build in the Swift 5.9 language mode.
- StoreKit 2.
The sources also build for visionOS and for app extensions, such as widgets.
Install
The Swift SDK installs with Swift Package Manager from the
Voidhash repository, whose releases are tagged with their
version, such as 0.3.0. In Xcode, choose File → Add Package Dependencies…, enter
https://github.com/voidhashcom/voidhash, set the dependency rule to Exact Version with the
latest release, and add Voidhash (and VoidhashCore when you need it) to your app target. In a
Package.swift:
dependencies: [
.package(url: "https://github.com/voidhashcom/voidhash", exact: "0.3.0")
],
targets: [
.target(
name: "App",
dependencies: [
.product(name: "Voidhash", package: "voidhash"),
.product(name: "VoidhashCore", package: "voidhash"),
]
)
]The SDK is an alpha release, and a new version can change its API. Pin an exact version and read the changelog before you update. See Versions and releases.
The SDK has three library products:
| Product | What it contains |
|---|---|
Voidhash | The client you integrate against: Voidhash.configure(…) returns a VoidhashClient. |
VoidhashCore | The shared native core. It defines the model and error types the SDK returns, such as SdkPerson, JSONValue and VoidhashStoreError. Add it and import VoidhashCore when your code names one of them. |
VoidhashUI | The native paywall renderer. Needed only to present paywalls. |
VoidhashUI is in the package from the first release that includes its prebuilt core; see
Add the paywall renderer.
The SDK isn't published for CocoaPods. The @voidhash/ios npm package, which earlier releases
offered for CocoaPods, is deprecated: install with Swift Package Manager instead.
Create the client
Configure one client while the app launches and keep it for the app's lifetime. In a SwiftUI app,
do it in the App initializer; in a UIKit app, in application(_:didFinishLaunchingWithOptions:).
import SwiftUI
import Voidhash
@main
struct MyApp: App {
init() {
Voidhash.configure(publishableKey: "vh_pk_...")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}Release builds, including TestFlight and App Review builds, use the production key. Debug builds in development mode use the test key. See API keys.
configure returns the client. Everywhere else, reach it through Voidhash.shared. The examples
in these docs call it voidhash.
guard let voidhash = Voidhash.shared else { return }Don't configure the client in a let at file scope. Swift creates such a global the first time
code reads it, so the SDK wouldn't observe transactions or record the launch until then.
Initialization runs in the background: it connects to StoreKit, loads the project's configuration
and syncs transactions that happened while the app was away. The first call that needs it waits
for it. To wait explicitly, for example behind a loading screen, call
try await voidhash.waitForInitialization(). It never fails because Voidhash is unreachable.
VoidhashClient(publishableKey:options:) creates a client without starting it; call start() or
waitForInitialization() on it. Prefer Voidhash.configure, which does both and sets shared. To pass options, see
Client options.
Generated slug types
The Voidhash CLI can write your project's product, perk and location slugs to a Swift file as enums, so a mistyped slug fails to compile instead of at run time. The CLI is a single program that doesn't need Node.js: install it with Homebrew or the install script. Without the file, pass slugs as strings; everything else works the same.
List the file in voidhash.json in your project's root:
{
"team": "acme",
"project": "my-app",
"types": [{ "language": "swift", "output": "App/VoidhashTypes.swift" }]
}Run voidhash types generate after you change slugs in Studio, and commit the file.
voidhash types check fails when the committed file is behind the project, which suits CI.
voidhash init writes both files for you in an Xcode project or a Swift
package. Add the generated file to your app's target if Xcode doesn't pick it up.
The file declares three enums with a case per slug:
/// The slugs of the project's products.
public enum VoidhashProductSlug: String, CaseIterable, Sendable {
case proAnnual = "pro-annual"
case proMonthly = "pro-monthly"
}
/// The slugs of the project's paywall locations.
public enum VoidhashLocationSlug: String, CaseIterable, Sendable {
case onboarding = "onboarding"
}
/// The slugs of the project's perks.
public enum VoidhashPerkSlug: String, CaseIterable, Sendable {
case premium = "premium"
}A slug that isn't a Swift name is spelled as one, so pro-monthly is proMonthly. The SDK takes
slugs as String, so pass a case's rawValue:
let result = await voidhash.presentFlow(placement: VoidhashLocationSlug.onboarding.rawValue)
try await voidhash.purchase(productSlug: VoidhashProductSlug.proMonthly.rawValue)
let hasPremium = try await voidhash.getCurrentPerson()?.hasActivePerk(VoidhashPerkSlug.premium.rawValue) ?? falsePrivacy manifest
VoidhashCore ships a privacy manifest that Xcode adds to your app's privacy report. It declares
no tracking, the UserDefaults API (reason CA92.1) and the system boot time API (reason 35F9.1),
and lists the data the SDK sends, all linked to the person and none used for tracking:
| Data type | Source | Purpose |
|---|---|---|
| User ID, device ID | The distinct ID | App functionality, analytics |
| Email address, name | What you pass to identify | App functionality |
| Purchase history | Store transactions | App functionality, analytics |
| Product interaction | Events, screens, lifecycle events | Analytics |
| Advertising data | The Apple Ads campaign behind the install | Analytics |
| Crash data | That the app crashed, and in which version; no crash logs | Analytics |
| Coarse location | City-level location derived from the request address | Analytics |
The last four are collected only while analytics consent is granted. Declare anything else you send, such as attributes or event properties that hold personal data, in your app's own privacy details.
Next steps
Something wrong or missing on this page? Tell us by email or on Discord.