Skip to content

Setting up the SDK

Installation

Install the Privy Swift SDK via the Swift Package Manager.

Open the following menu item in Xcode: File > Add Package Dependencies...

In the Search or Enter Package URL search box enter this URL:

https://github.com/privy-io/privy-ios

Initialization

First, import the Privy SDK by adding on top of the file.

swift
import PrivySDK

Next, initialize the Privy client with a PrivyConfig object containing an appId field with your Privy app ID as a string. Your app ID can be obtained from the Privy Developer Dashboard under Settings > Basics, and your client id can be obtained under Settings > Clients.

swift
let config = PrivyConfig(appId: "<your-app-id>", appClientId: "<client-id>")
let privy: Privy = PrivySdk.initialize(config: config)

TIP

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.

When the PrivySDK is initialized, Privy will try to restore the user's previous session if they were previously logged in. During this time, the user's authentication state will be notReady. We recommend rendering a loading state in your UI until AuthState changes from notReady, at which point it's safe to use the remainder of the Privy SDK.

swift
var isPrivySdkReady = false

privy.setAuthStateChangeCallback { state in
    if !self.isPrivySDKReady && state != AuthState.notReady {
        isPrivySdkReady = true
    }
}

// later in the view
if isPrivySdkReady {
    AppView()
} else {
    LoadingView()
}

Optionally, you can use the onAuthStateChange onEmbeddedWalletStateChange callbacks to be notified when authentication or wallet state changes.

swift
privy.setAuthStateChangeCallback { state in
    // Logic to execute after there is an auth change.
    self.myAuthState = state
)

privy.setEmbeddedWalletStateChangeCallback { state in
    // Logic to execute after there is an auth change.
    self.myEmbeddedWalletState = state
)

That's it! You can now use the Privy SDK to securely authenticate and provision embedded wallets for your users. 🎉