Skip to content

For iOS Developers

This guide will walk you through the process of setting up AptosKit, and fetching data on the Aptos blockchain.

  1. Install the SDK

    AptosKit is available as a Swift package. To add it to your project, add the following to your Package.swift file:

    dependencies: [
    .package(url: "https://github.com/mcxross/swift-aptos.git", .upToNextMajor(from: <version>))
    ]
  2. Import the SDK

    Import the SDK in your Swift file:

    import AptosKit
  3. Create the ClientConfig object

    This object is used to configure the client behavior. You can set maxRetries, requestTimeout, and retryOnServerErrors properties.

    let config = ClientConfig(
    followRedirects: true,
    agent: "AptosClient",
    likeAgent: nil,
    requestTimeout: 5000,
    retryOnServerErrors: 3,
    maxRetries: 5,
    cache: false,
    proxy: nil
    )
  4. Create the AptosSettings object

    This object is used to configure the Aptos network connection. You can set network, fullnode, and faucet properties.

    let aptosSettings = AptosSettings(
    network: .devnet,
    fullNode: nil,
    faucet: nil,
    indexer: nil,
    client: nil,
    clientConfig: config,
    fullNodeConfig: nil,
    indexerConfig: nil,
    faucetConfig: nil
    )
  5. Create the AptosConfig object

    let aptosConfig = AptosConfig(settings: aptosSettings)
  6. Create the Aptos object

    This object is used to interact with the Aptos blockchain. It serves as the entry point for all interactions with the blockchain.

    let aptos = Aptos(config: aptosConfig, graceFull: false)
  7. Fetch the chain ID

    let chainId = try await aptos.getChainId()

    Congratulations! You have successfully set up the AptosKit SDK and fetched the chain ID from the Aptos blockchain.

import SwiftUI
import AptosKit
struct ContentView: View {
@State private var chainId: String? = nil
var body: some View {
VStack {
if let chainId = chainId {
Text("Chain ID: \(chainId)")
} else {
Text("Fetching Chain ID...")
}
}
.padding()
.onAppear {
fetchChainId()
}
}
private func fetchChainId() {
DispatchQueue.main.async {
Task {
do {
let clientConfig = ClientConfig(
followRedirects: true,
agent: "AptosClient",
likeAgent: nil,
requestTimeout: 5000,
retryOnServerErrors: 3,
maxRetries: 5,
cache: false,
proxy: nil
)
let aptosSettings = AptosSettings(
network: .devnet,
fullNode: nil,
faucet: nil,
indexer: nil,
client: nil,
clientConfig: clientConfig,
fullNodeConfig: nil,
indexerConfig: nil,
faucetConfig: nil
)
let aptosConfig = AptosConfig(settings: aptosSettings)
let aptos = Aptos(config: aptosConfig, graceFull: false)
let chainId = try await aptos.getChainId()
self.chainId = chainId.expect(message: "Failed...")?.stringValue ?? "null"
} catch {
print("Failed to get chain ID: \(error)")
self.chainId = "Error"
}
}
}
}
}