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.

NEARAccount

The NEARAccount struct represents an authenticated NEAR blockchain account. It contains the account identifier, public key, and wallet information returned after a successful connection.

Overview

NEARAccount is a value type that conforms to Codable, Equatable, Hashable, and Sendable. It is automatically persisted by NEARWalletManager and restored on app launch.
import NEARConnect

public struct NEARAccount: Codable, Equatable, Hashable, Sendable

Properties

accountId

The NEAR account identifier.
public let accountId: String
accountId
String
The unique NEAR account ID (e.g., “alice.near”, “bob.testnet”).
Example:
if let account = walletManager.currentAccount {
    print("Connected as: \(account.accountId)")
}

publicKey

The account’s public key.
public let publicKey: String?
publicKey
String?
The public key associated with this account, or nil if not provided by the wallet.
Example:
if let key = walletManager.currentAccount?.publicKey {
    print("Public key: \(key)")
}

walletId

The identifier of the wallet that authenticated this account.
public let walletId: String
walletId
String
The wallet identifier (e.g., “meteor-wallet”, “my-near-wallet”, “near-wallet”).
Example:
if let account = walletManager.currentAccount {
    print("Connected via: \(account.walletId)")
}

Computed Properties

displayName

The full account identifier for display purposes.
public var displayName: String
displayName
String
Returns the accountId unchanged. Use this for displaying the full account name.
Example:
Text("Welcome, \(account.displayName)")

shortDisplayName

A truncated version of the account identifier for compact display.
public var shortDisplayName: String
shortDisplayName
String
If the account ID is longer than 24 characters, returns a truncated version showing the first 12 and last 8 characters separated by ”…”. Otherwise returns the full account ID.
Example:
// For "alice.near" -> "alice.near"
// For "verylongaccountname123456.near" -> "verylongacco...56.near"

Text(account.shortDisplayName)
    .font(.caption)

Initialization

init(accountId:publicKey:walletId:)

Creates a new NEARAccount instance.
public init(
    accountId: String,
    publicKey: String?,
    walletId: String
)
accountId
String
required
The NEAR account identifier.
publicKey
String?
required
The account’s public key, or nil if not available.
walletId
String
required
The wallet identifier that authenticated this account.
Example:
let account = NEARAccount(
    accountId: "alice.near",
    publicKey: "ed25519:5FwoV...",
    walletId: "meteor-wallet"
)
Note: You typically won’t create NEARAccount instances manually. They are returned by NEARWalletManager after successful wallet connection.

Usage in SwiftUI

Since NEARAccount is Codable, Equatable, Hashable, and Sendable, it works seamlessly with SwiftUI and Combine. Example:
import SwiftUI
import NEARConnect

struct ProfileView: View {
    @EnvironmentObject var walletManager: NEARWalletManager
    
    var body: some View {
        if let account = walletManager.currentAccount {
            VStack(alignment: .leading, spacing: 12) {
                Label {
                    Text(account.displayName)
                        .font(.headline)
                } icon: {
                    Image(systemName: "person.circle.fill")
                        .foregroundColor(.blue)
                }
                
                HStack {
                    Text("Wallet:")
                        .foregroundColor(.secondary)
                    Text(account.walletId)
                        .fontWeight(.medium)
                }
                
                if let publicKey = account.publicKey {
                    VStack(alignment: .leading) {
                        Text("Public Key:")
                            .font(.caption)
                            .foregroundColor(.secondary)
                        Text(publicKey.prefix(20) + "...")
                            .font(.caption2)
                            .foregroundColor(.secondary)
                    }
                }
                
                Button("Disconnect") {
                    walletManager.disconnect()
                }
                .buttonStyle(.bordered)
            }
            .padding()
        } else {
            Button("Connect Wallet") {
                walletManager.connect()
            }
            .buttonStyle(.borderedProminent)
        }
    }
}

Persistence

NEARAccount is automatically persisted by NEARWalletManager using UserDefaults. When the app relaunches, the last connected account is restored. Example:
// Account persists across app launches
let walletManager = NEARWalletManager()

if walletManager.isSignedIn {
    print("Restored account: \(walletManager.currentAccount?.accountId ?? "")")
} else {
    print("No account connected")
}

Conformances

Codable

Allows NEARAccount to be encoded/decoded for persistence.
let encoder = JSONEncoder()
let data = try encoder.encode(account)

let decoder = JSONDecoder()
let restored = try decoder.decode(NEARAccount.self, from: data)

Equatable

Enables comparison between account instances.
if account1 == account2 {
    print("Same account")
}

Hashable

Allows NEARAccount to be used in sets and as dictionary keys.
var accounts: Set<NEARAccount> = []
accounts.insert(account)

let accountMap: [NEARAccount: String] = [
    account1: "Primary",
    account2: "Secondary"
]

Sendable

Marks NEARAccount as safe to pass across concurrency domains.
Task {
    await processAccount(account) // Safe to use across async boundaries
}

See Also