Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/frol/near-connect-ios/llms.txt

Use this file to discover all available pages before exploring further.

Overview

This guide shows you how to set up NEAR Connect in your iOS app from scratch. You’ll learn how to configure the wallet manager, environment objects, and wallet UI sheet.
The code examples on this page are extracted from the complete working example app included with NEAR Connect iOS SDK.

Complete App Setup

1
Create the App Entry Point
2
Set up your main app with NEARWalletManager as a @StateObject and inject it into the environment:
3
import SwiftUI
import NEARConnect

@main
struct NEARConnectExampleApp: App {
    @StateObject private var walletManager = NEARWalletManager()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(walletManager)
        }
    }
}
4
Using @StateObject ensures the wallet manager persists for the lifetime of your app and survives view updates.
5
Create the Main Content View
6
Access the wallet manager using @EnvironmentObject and set up the WalletBridgeSheet:
7
import SwiftUI
import NEARConnect

struct ContentView: View {
    @EnvironmentObject var walletManager: NEARWalletManager
    @State private var showError = false
    @State private var errorMessage = ""

    var body: some View {
        NavigationView {
            VStack(spacing: 30) {
                headerView

                if walletManager.isSignedIn, let account = walletManager.currentAccount {
                    // Show connected account UI
                    accountView(account)
                } else {
                    // Show connect prompt
                    connectPrompt
                }
            }
            .padding()
            .navigationBarTitleDisplayMode(.inline)
            // CRITICAL: Present WalletBridgeSheet when showWalletUI is true
            .fullScreenCover(isPresented: $walletManager.showWalletUI) {
                WalletBridgeSheet()
                    .environmentObject(walletManager)
            }
            .alert("Error", isPresented: $showError) {
                Button("OK", role: .cancel) { }
            } message: {
                Text(errorMessage)
            }
        }
    }

    private var headerView: some View {
        VStack(spacing: 16) {
            Image(systemName: "link.circle.fill")
                .font(.system(size: 80))
                .foregroundStyle(
                    LinearGradient(
                        colors: [.blue, .purple],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )

            Text("NEAR Connect")
                .font(.largeTitle)
                .fontWeight(.bold)

            Text("iOS Demo")
                .font(.title2)
                .foregroundColor(.secondary)
        }
    }

    private func accountView(_ account: NEARAccount) -> some View {
        VStack(spacing: 24) {
            VStack(spacing: 16) {
                Image(systemName: "person.circle.fill")
                    .font(.system(size: 60))
                    .foregroundColor(.green)

                VStack(spacing: 8) {
                    Text("Connected")
                        .font(.headline)
                        .foregroundColor(.secondary)

                    Text(account.accountId)
                        .font(.title3)
                        .fontWeight(.semibold)
                        .multilineTextAlignment(.center)
                }

                HStack {
                    Image(systemName: "wallet.pass.fill")
                        .font(.caption)
                    Text(account.walletId)
                        .font(.caption)
                }
                .padding(.horizontal, 12)
                .padding(.vertical, 6)
                .background(Color.blue.opacity(0.1))
                .cornerRadius(12)
            }
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 20)
                    .fill(Color(uiColor: .systemBackground))
                    .shadow(color: .black.opacity(0.1), radius: 10, x: 0, y: 5)
            )

            Button(action: {
                walletManager.disconnect()
            }) {
                Label("Disconnect", systemImage: "rectangle.portrait.and.arrow.right")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.red)
                    .cornerRadius(12)
            }
        }
        .padding()
    }

    private var connectPrompt: some View {
        VStack(spacing: 20) {
            Text("Connect your NEAR wallet to get started")
                .font(.title3)
                .multilineTextAlignment(.center)
                .foregroundColor(.secondary)
                .padding(.horizontal)

            Button(action: { walletManager.connect() }) {
                Label("Connect Wallet", systemImage: "wallet.pass")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(
                        LinearGradient(
                            colors: [.blue, .purple],
                            startPoint: .leading,
                            endPoint: .trailing
                        )
                    )
                    .cornerRadius(12)
            }
            .shadow(color: .blue.opacity(0.3), radius: 10, x: 0, y: 5)
        }
        .padding()
    }
}
8
Understanding the Key Components
9
@EnvironmentObject
@EnvironmentObject var walletManager: NEARWalletManager
Access the wallet manager instance passed from the parent view. This gives you access to all wallet functionality throughout your view hierarchy.
WalletBridgeSheet
.fullScreenCover(isPresented: $walletManager.showWalletUI) {
    WalletBridgeSheet()
        .environmentObject(walletManager)
}
The WalletBridgeSheet is the critical component that handles the WebView bridge to NEAR wallets. It automatically shows when showWalletUI becomes true (when wallet interaction is needed).
Connection State
if walletManager.isSignedIn, let account = walletManager.currentAccount {
    // User is connected
    Text(account.accountId)
} else {
    // User needs to connect
    Button("Connect") { walletManager.connect() }
}
Check isSignedIn to determine connection state and access currentAccount for account details.

Key Properties and Methods

NEARWalletManager Properties

PropertyTypeDescription
isSignedInBoolWhether a wallet is currently connected
currentAccountNEARAccount?The currently connected account (if any)
showWalletUIBoolControls when to present the WalletBridgeSheet

NEARWalletManager Methods

MethodDescription
connect()Initiates wallet connection flow
disconnect()Disconnects the current wallet

NEARAccount Properties

struct NEARAccount {
    let accountId: String      // NEAR account ID (e.g., "alice.near")
    let walletId: String       // Wallet identifier
    let publicKey: String?     // Account's public key (optional)
}
Always use fullScreenCover (not sheet) for the WalletBridgeSheet to ensure proper wallet UI presentation.

Next Steps

Token Transfer

Learn how to send NEAR tokens

Smart Contracts

Call smart contract functions

Authentication

Implement sign-in with message signing

API Reference

Complete API documentation

Build docs developers (and LLMs) love