> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crossmint.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift Quickstart

> Let users withdraw stablecoins to their bank account from your iOS app

In this guide, you will add a withdrawal flow to a SwiftUI app with the `CrossmintEmbeddedOfframp` component and run a withdrawal in the Staging environment. You will:

* Create an offramp session on your server for the user and the wallet they withdraw from
* Render `CrossmintEmbeddedOfframp` with the session and a payer that signs with the user's wallet
* Handle the component's events
* Test a withdrawal on staging

The component runs the whole withdrawal inside your app: the user picks, adds, renames or removes a bank account, enters an amount, verifies their identity if the order needs it, reviews the quote and approves the deposit from their wallet.

<Snippet file="offramp-embedded-flow.mdx" />

## Prerequisites

* **Backend**: one server endpoint that creates the session, such as the Express route in this guide.
* **iOS**: iOS 15 or later, and Xcode 26 or later.

<Snippet file="offramp-embedded-prereqs.mdx" />

* **Wallet**: an EVM or Solana wallet that your app can sign and broadcast transactions with, owned by or linked to `userId:<USER_ID>`. To use a wallet that Crossmint did not create, link it to the user with [Link External Wallet to User](https://docs.crossmint.com/api-reference/users/link-wallet.md) before you create the session. The session takes only `userId:` locators, so email and phone locators do not work. This guide uses `base-sepolia`.
* **USDC**: testnet USDC in the wallet, from the [Circle faucet](https://faucet.circle.com/).
* **Camera access**: identity verification captures documents with the camera. Set `NSCameraUsageDescription` in your app's `Info.plist`.

Add the Crossmint Checkout Swift package, version 1.5.0 or later, with Swift Package Manager. In Xcode, go to **File > Add Package Dependencies**, enter the repository URL, and add the `CrossmintCheckout` product to your app target:

```text theme={null}
https://github.com/Crossmint/crossmint-checkout-swift
```

### 1. Create the session on your server

A session lets one user make one withdrawal from one wallet. Your backend makes one call, [Create Offramp Session](https://docs.crossmint.com/offramp/api-reference/create-offramp-session.md), and the component runs every other step with Crossmint:

<Snippet file="offramp-embedded-where-it-runs.mdx" />

<Snippet file="offramp-embedded-session-backend.mdx" />

<Snippet file="offramp-embedded-session-fields.mdx" />

<Snippet file="offramp-embedded-https-backend.mdx" />

### 2. Render the offramp component

Create a view that asks your backend for a session and renders `CrossmintEmbeddedOfframp`. The `payer` signs and sends the deposit with your wallet code, which the view takes as `sendDeposit`. With `fill: true`, the offramp fills the screen and scrolls inside itself:

```swift WithdrawView.swift theme={null}
import CrossmintCheckout
import SwiftUI

// Replace with your client-side API key and your backend URL.
let crossmintApiKey = "YOUR_CLIENT_API_KEY"
let sessionURL = URL(string: "https://YOUR_BACKEND_URL/api/offramp-session")!

struct WithdrawView: View {
    let userId: String
    let email: String
    let walletAddress: String
    let chain: String
    let sendDeposit: @MainActor (OfframpTransaction) async throws -> String

    @State private var credentials: EmbeddedOfframpCredentials?
    @State private var isLoading = false
    @State private var isSigning = false

    var body: some View {
        if let credentials {
            VStack(spacing: 0) {
                Button("Close") { self.credentials = nil }
                    .disabled(isSigning)
                    .frame(maxWidth: .infinity, alignment: .trailing)
                    .padding()
                CrossmintEmbeddedOfframp(
                    apiKey: crossmintApiKey,
                    credentials: credentials,
                    payer: EmbeddedOfframpPayer(signAndSendTransaction: { transaction in
                        isSigning = true
                        defer { isSigning = false }
                        return try await sendDeposit(transaction)
                    }),
                    fill: true
                )
                .onStatusChange { change in
                    print("Offramp status:", change.status.rawValue, change.orderId ?? "none")
                }
                .onComplete { completion in
                    print("Withdrawal sent:", completion.orderId, completion.txId)
                }
                .onCancel { self.credentials = nil }
                .onError { error in
                    print("Offramp error:", error.reason.rawValue, error.retriable, error.message)
                }
            }
        } else {
            Button(isLoading ? "Starting..." : "Withdraw to bank") {
                Task { await startWithdrawal() }
            }
            .disabled(isLoading)
        }
    }

    private func startWithdrawal() async {
        isLoading = true
        defer { isLoading = false }
        do {
            var request = URLRequest(url: sessionURL)
            request.httpMethod = "POST"
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = try JSONEncoder().encode([
                "userId": userId,
                "receiptEmail": email,
                "walletAddress": walletAddress,
                "chain": chain,
            ])
            let (data, response) = try await URLSession.shared.data(for: request)
            let status = (response as? HTTPURLResponse)?.statusCode ?? 0
            guard status == 200 else {
                throw URLError(.badServerResponse)
            }
            credentials = try JSONDecoder().decode(EmbeddedOfframpCredentials.self, from: data)
        } catch {
            print("Failed to create the offramp session:", error)
        }
    }
}
```

Render `WithdrawView` with the signed-in user's ID and email, the address and chain of their wallet, and your wallet code as `sendDeposit`. The component calls `sendDeposit` with the deposit to send:

* **`serializedTransaction`**: a serialized transaction on EVM chains, or a base58 legacy transaction with `feePayer` and `recentBlockhash` set on Solana. Sign and broadcast it unchanged.
* **`chain`** and **`payerAddress`**: the chain and wallet the deposit was prepared for. Refuse to send from a wallet that does not match. See [Refuse a mismatched wallet](https://docs.crossmint.com/offramp/guides/handle-events-and-errors.md#refuse-a-mismatched-wallet).

Return the transaction hash, or the signature on Solana. Throw `OfframpSignatureRejectedError` only when the user declines and nothing was broadcast, so the component can offer the withdrawal again with a new rate. Any other error counts as an unknown outcome: the component waits for the deposit instead of letting the user send it twice. **Close** stays disabled while `sendDeposit` runs, because the component has to stay on screen to pass your wallet's answer back to the offramp.

Without `fill`, the component takes the height of the page, at least 650px plus padding, and grows when a screen needs more. See [Sizing](https://docs.crossmint.com/offramp/guides/ui-customization.md#sizing).

### 3. Handle the events

<Snippet file="offramp-embedded-handle-events.mdx" />

### 4. Test on staging

<Snippet file="offramp-embedded-test-on-staging.mdx" />

## Prove Wallet Ownership

An order can need proof that the user owns the wallet. To let the component ask for it, pass `signMessage` to the payer: `EmbeddedOfframpPayer(signAndSendTransaction:signMessage:)`. The component calls it with a Crossmint sign-in challenge for `payerAddress`. Return the EIP-191 `personal_sign` signature as a 0x-prefixed hex string on EVM chains, or the base64 encoding of the 64-byte ed25519 signature over the UTF-8 message on Solana. Adding or removing `signMessage` reloads the offramp.

Without `signMessage`, the component stops with the `wallet-ownership-required` error. Link the wallet again with an ownership `proof`, then create a new session. For Solana deposits, see [Offramp Orders on Solana](https://docs.crossmint.com/offramp/guides/sign-deposit-solana.md).

## Troubleshooting

<Snippet file="offramp-embedded-troubleshooting.mdx" />

## Launching in Production

<Snippet file="offramp-embedded-launch.mdx" />

## Learn More

> [card] [Swift SDK Reference](https://docs.crossmint.com/sdk-reference/checkout/swift/structs/crossmintembeddedofframp.md)
>
> Parameters and handlers of `CrossmintEmbeddedOfframp`

> [card] [Handle Offramp Events and Errors](https://docs.crossmint.com/offramp/guides/handle-events-and-errors.md)
>
> What each status and error reason means
