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

NEAR Connect iOS provides a seamless wallet connection experience through the NEARWalletManager. The manager handles wallet selection, authentication, and session persistence automatically.

Connect a Wallet

1

Present the wallet selector

Call connect() to show the wallet selector UI:
walletManager.connect()
This triggers the wallet selector modal where users can choose from available NEAR wallets (MyNEARWallet, Meteor, HERE Wallet, etc.).
2

Display the wallet UI

The NEARWalletManager automatically sets showWalletUI = true. Present the WalletBridgeSheet in your view:
.fullScreenCover(isPresented: $walletManager.showWalletUI) {
    WalletBridgeSheet()
        .environmentObject(walletManager)
}
3

Check connection status

Monitor the connection state using published properties:
if walletManager.isSignedIn, let account = walletManager.currentAccount {
    Text("Connected: \(account.accountId)")
    Text("Wallet: \(account.walletId)")
    if let publicKey = account.publicKey {
        Text("Public Key: \(publicKey)")
    }
}

Connect to Specific Wallet

To connect directly to a specific wallet without showing the selector:
// Connect to MyNEARWallet
walletManager.connect(walletId: "my-near-wallet")

// Connect to Meteor Wallet
walletManager.connect(walletId: "meteor-wallet")

// Connect to HERE Wallet
walletManager.connect(walletId: "here-wallet")
Wallet IDs are standardized across the NEAR ecosystem. Common wallet IDs include:
  • my-near-wallet
  • meteor-wallet
  • here-wallet
  • sender-wallet

Session Persistence

Connected accounts are automatically persisted to UserDefaults and restored when your app relaunches.
// The manager loads the stored account on initialization
let walletManager = NEARWalletManager()

// Account is automatically available if previously connected
if let account = walletManager.currentAccount {
    print("Restored session for \(account.accountId)")
}

Custom Storage

You can provide a custom UserDefaults instance for multi-user scenarios:
let customDefaults = UserDefaults(suiteName: "com.myapp.user1")
let walletManager = NEARWalletManager(userDefaults: customDefaults)

Disconnect Wallet

To disconnect the current wallet and clear the session:
walletManager.disconnect()
This:
  • Clears currentAccount to nil
  • Removes the stored session from UserDefaults
  • Notifies the wallet’s JavaScript bridge
  • Resets any error state

Complete Example

Here’s a complete connection flow from the example app:
import SwiftUI
import NEARConnect

struct ContentView: View {
    @EnvironmentObject var walletManager: NEARWalletManager
    
    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                if walletManager.isSignedIn, let account = walletManager.currentAccount {
                    // Connected state
                    VStack(spacing: 16) {
                        Text("Connected")
                            .font(.headline)
                        
                        Text(account.accountId)
                            .font(.title3)
                            .fontWeight(.semibold)
                        
                        HStack {
                            Image(systemName: "wallet.pass.fill")
                            Text(account.walletId)
                        }
                        .font(.caption)
                        
                        Button(action: {
                            walletManager.disconnect()
                        }) {
                            Label("Disconnect", systemImage: "rectangle.portrait.and.arrow.right")
                                .foregroundColor(.white)
                                .frame(maxWidth: .infinity)
                                .padding()
                                .background(Color.red)
                                .cornerRadius(12)
                        }
                    }
                } else {
                    // Disconnected state
                    VStack(spacing: 20) {
                        Text("Connect your NEAR wallet to get started")
                            .multilineTextAlignment(.center)
                        
                        Button(action: { walletManager.connect() }) {
                            Label("Connect Wallet", systemImage: "wallet.pass")
                                .foregroundColor(.white)
                                .frame(maxWidth: .infinity)
                                .padding()
                                .background(Color.blue)
                                .cornerRadius(12)
                        }
                    }
                }
            }
            .padding()
            .fullScreenCover(isPresented: $walletManager.showWalletUI) {
                WalletBridgeSheet()
                    .environmentObject(walletManager)
            }
        }
    }
}

Network Selection

Switch between mainnet and testnet:
// Set network before connecting
walletManager.network = .testnet  // or .mainnet
walletManager.connect()
The network must be set before the wallet connects. Changing the network after connection requires disconnecting and reconnecting.

Monitoring Connection State

The NEARWalletManager publishes several state properties:
@Published public var currentAccount: NEARAccount?
@Published public var isBusy: Bool
@Published public var lastError: String?
@Published public var showWalletUI: Bool
@Published public var network: Network

public var isSignedIn: Bool { currentAccount != nil }
Use these to build reactive UIs:
struct StatusView: View {
    @EnvironmentObject var walletManager: NEARWalletManager
    
    var body: some View {
        VStack {
            if walletManager.isBusy {
                ProgressView("Processing...")
            }
            
            if let error = walletManager.lastError {
                Text("Error: \(error)")
                    .foregroundColor(.red)
            }
            
            Text("Network: \(walletManager.network.rawValue)")
                .foregroundColor(.secondary)
        }
    }
}

Best Practices

Single Manager Instance: Create one NEARWalletManager instance and share it via @EnvironmentObject across your app. The manager maintains a persistent WebView bridge that should live for the app’s lifetime.
@main
struct MyApp: App {
    @StateObject private var walletManager = NEARWalletManager()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(walletManager)
        }
    }
}

Error Handling

Connection errors are communicated through the lastError property:
struct ConnectionView: View {
    @EnvironmentObject var walletManager: NEARWalletManager
    @State private var showError = false
    
    var body: some View {
        Button("Connect") {
            walletManager.connect()
        }
        .onChange(of: walletManager.lastError) { error in
            if error != nil {
                showError = true
            }
        }
        .alert("Connection Error", isPresented: $showError) {
            Button("OK") { }
        } message: {
            Text(walletManager.lastError ?? "Unknown error")
        }
    }
}

Next Steps

Build docs developers (and LLMs) love