Skip to content

Repository files navigation

Dynamic Swift SDK

iOS SDK for Dynamic's authentication and Web3 wallet infrastructure.

Features

  • Authentication: Social (Google, Apple, Farcaster), Email/SMS OTP, Passkey, External JWT
  • Multi-Chain Wallets: EVM (Ethereum, Polygon, Base, etc.) and Solana
  • Wallet Operations: Balances, signing, transactions, network switching
  • Security: MFA/TOTP, Passkey management, Recovery codes
  • Reactive State: Combine framework integration

Requirements

  • iOS 15.0+
  • Xcode 16.0+
  • Swift 5.9+

Installation

Swift Package Manager

Add the SDK to your project using one of the following methods:

Option 1: Xcode UI

  1. Open your project in Xcode
  2. Go to File > Add Package Dependencies...
  3. Enter the repository URL:
    https://github.com/dynamic-labs/swift-sdk-and-sample-app
    
  4. Select the version rule (e.g., Up to Next Major Version from 1.2.0)
  5. Click Add Package
  6. Select DynamicSDKSwift library and add it to your target

Option 2: Package.swift

Add the dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/dynamic-labs/swift-sdk-and-sample-app.git", from: "1.2.0")
]

Then add DynamicSDKSwift to your target's dependencies:

.target(
    name: "YourApp",
    dependencies: [
        .product(name: "DynamicSDKSwift", package: "DynamicSDKSwift")
    ]
)

Framework Embedding

The SDK includes a dynamic framework (DynamicSDKSwift.xcframework) that must be embedded in your app. If you're using SPM, Xcode handles this automatically. If you're integrating manually:

  1. Drag DynamicSDKSwift.xcframework into your project's Frameworks, Libraries, and Embedded Content section
  2. Set DynamicSDKSwift.xcframework to Embed & Sign
  3. Set the stub frameworks (SwiftBigInt, SolanaWeb3, AnyCodableSwift) to Do Not Embed

Quick Start

Initialize

import SwiftUI
import DynamicSDKSwift

@main
struct YourApp: App {
    init() {
        _ = DynamicSDK.initialize(
            props: ClientProps(
                environmentId: "YOUR_ENVIRONMENT_ID",
                appLogoUrl: "https://yourdomain.com/logo.png",
                appName: "Your App Name",
                redirectUrl: "yourapp://",
                appOrigin: "https://yourdomain.com"
            )
        )
    }

    var body: some Scene {
        WindowGroup { ContentView() }
    }
}

Access SDK

let sdk = try DynamicSDK.getInstance()

Authentication

Built-in UI

sdk.ui.showAuth()

Social Providers

try await sdk.auth.social.connect(provider: .google)  // or .apple, .farcaster

Email OTP

try await sdk.auth.email.sendOTP(email: "user@example.com")
try await sdk.auth.email.verifyOTP(token: "123456")

SMS OTP

let phoneData = PhoneData(countryCode: "1", phoneNumber: "5555551234")
try await sdk.auth.sms.sendOTP(phoneData: phoneData)
try await sdk.auth.sms.verifyOTP(token: "123456")

Passkey

try await sdk.auth.passkey.signIn()
try await sdk.passkeys.registerPasskey()

External JWT

try await sdk.auth.externalAuth.signInWithExternalJwt(
    props: SignInWithExternalJwtParams(jwt: "your-jwt-token")
)

Auth State

let user = sdk.auth.authenticatedUser

sdk.auth.authenticatedUserChanges
    .receive(on: DispatchQueue.main)
    .sink { user in /* handle user changes */ }
    .store(in: &cancellables)

try await sdk.auth.logout()

Wallet Operations

Get Wallets & Balance

let wallets = sdk.wallets.userWallets
let balance = try await sdk.wallets.getBalance(wallet: wallet)
let network = try await sdk.wallets.getNetwork(wallet: wallet)

Network & Primary Wallet

try await sdk.wallets.switchNetwork(wallet: wallet, network: targetNetwork)
try await sdk.wallets.setPrimary(walletId: walletId)

Sign Message

let signature = try await sdk.wallets.signMessage(wallet: wallet, message: "Hello!")

EVM Transactions

Send Transaction

let client = try await sdk.evm.createPublicClient(chainId: chainId)
let gasPrice = try await client.getGasPrice()

let transaction = EthereumTransaction(
    from: wallet.address,
    to: recipientAddress,
    value: BigUInt("1000000000000000000"),  // 1 ETH in Wei
    gas: BigUInt(21000),
    maxFeePerGas: gasPrice * 2,
    maxPriorityFeePerGas: gasPrice * 2
)

let txHash = try await sdk.evm.sendTransaction(transaction: transaction, wallet: wallet)

Sign Typed Data (EIP-712)

let signature = try await sdk.wallets.signTypedData(wallet: wallet, typedDataJson: typedDataJson)

ERC20 & Contract Interactions

let input = WriteContractInput(
    contractAddress: tokenContractAddress,
    functionName: "transfer",
    args: [recipientAddress, tokenAmount],
    abi: Erc20.abi
)

let txHash = try await sdk.evm.writeContract(wallet: wallet, input: input)

Solana Transactions

Sign & Send

let connection = try await sdk.solana.createConnection()
let signer = try await sdk.solana.createSigner(wallet: wallet)
let blockhash = try await connection.getLatestBlockhash()

let signature = try await signer.signAndSendEncodedTransaction(base64Transaction: base64Tx)

MFA

Device Management

let devices = try await sdk.mfa.getUserDevices()

// Add TOTP device
let device = try await sdk.mfa.addDevice(type: .totp)
try await sdk.mfa.verifyDevice(code: "123456", type: .totp)

// Delete device
let token = try await sdk.mfa.createMfaToken(singleUse: true)
try await sdk.mfa.deleteUserDevice(deviceId: deviceId, mfaAuthToken: token)

Recovery Codes

let codes = try await sdk.mfa.getRecoveryCodes(generateNewCodes: false)
try await sdk.mfa.authenticateRecoveryCode(code: recoveryCode)

Passkey Management

let passkeys = try await sdk.passkeys.getPasskeys()
try await sdk.passkeys.registerPasskey()
try await sdk.passkeys.deletePasskey(DeletePasskeyRequest(passkeyPublicKeyId: passkeyId))

User Profile

sdk.ui.showUserProfile()

if let user = sdk.auth.authenticatedUser {
    print("User ID: \(user.userId)")
    print("Email: \(user.email ?? "N/A")")
}

Networks

let evmNetworks = sdk.networks.evm      // Ethereum, Polygon, Arbitrum, Base, etc.
let solanaNetworks = sdk.networks.solana // Mainnet, Devnet, Testnet

Example App

  1. Open ExampleApp/DynamicSDKExample.xcodeproj
  2. Update environmentId in DynamicSDKExampleApp.swift
  3. Run on iOS Simulator or device

Demonstrates all auth flows, wallet operations, EVM/Solana transactions, MFA, and passkeys.

Support

About

Official Dynamic SDK for Swift

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages