Prerequisites

Before you begin, make sure you have set up your Privy app and obtained your app ID and client ID from the Privy Dashboard.

A properly set up app client is required for mobile apps and other non-web platforms to allow your app to interact with the Privy API. Please follow this guide to configure an app client by following this guide here.

Initializing Privy

First, import the Privy SDK at the top of the file:

import PrivySDK

Initialize a Privy instance with a PrivyConfig object:

let config = PrivyConfig(
    appId: "YOUR_APP_ID",
    appClientId: "YOUR_APP_CLIENT_ID",
    loggingConfig: .init(
        logLevel: .verbose
    )
)

let privy: Privy = PrivySdk.initialize(config: config)

Configuration

The configuration fields for the PrivyConfig are:

appId
String
required

Your Privy application ID, which can be obtained from the Privy Developer Dashboard, under App Settings > Basics

appClientId
String
required

Your app client ID, which can be obtained from the Privy Developer Dashboard, under App Settings > Clients

loggingConfig
PrivyLoggingConfig

(Optional) Your preferred log level and logging method. If no log level is specified, it will default to PrivyLogLevel.NONE.

customAuthConfig
LoginWithCustomAuthConfig

(Optional) Only use this if you plan to use custom authentication. Find more information here.

Be sure to maintain a single instance of Privy across the lifetime of your application. Initializing multiple instances of Privy will result in unexpected errors.

Waiting for Privy to be ready

When the Privy SDK is initialized, the user’s authentication state will be set to NotReady until Privy finishes initialization. This might include checking if the user has a wallet connected, refreshing expired auth tokens, fetching up-to-date user data, and more.

It’s important to wait until Privy has finished initializing before you consume Privy’s state and interfaces, to ensure that the state you consume is accurate and not stale.

For your convenience, we’ve added an async awaitReady() function that you can use to wait for Privy to finish initializing:

private func awaitPrivySDKReady() {
    Task {
        // Show loading UI

        // Await privy ready
        await privy.awaitReady()

        print("Privy SDK is ready!")

        // Check user auth state
        if case .authenticated(let privyUser) = privy.authState {
            // User is authenticated
        } else {
            // User is not authenticated
        }
    }
}