Saltearse al contenido

Comenzando con Kaptos Para Desarrolladores iOS

Esta guía te llevará a través del proceso de configurar AptosKit, y obtener datos en la blockchain de Aptos.

  1. Instalar el SDK

    AptosKit está disponible como un paquete Swift. Para agregarlo a tu proyecto, agrega lo siguiente a tu archivo Package.swift:

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

    Importa el SDK en tu archivo Swift:

    import AptosKit
  3. Crear el objeto ClientConfig

    Este objeto se usa para configurar el comportamiento del cliente. Puedes establecer propiedades maxRetries, requestTimeout, y retryOnServerErrors.

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

    Este objeto se usa para configurar la conexión de red de Aptos. Puedes establecer propiedades network, fullnode, y faucet.

    let aptosSettings = AptosSettings(
    network: .devnet,
    fullNode: nil,
    faucet: nil,
    indexer: nil,
    client: nil,
    clientConfig: config,
    fullNodeConfig: nil,
    indexerConfig: nil,
    faucetConfig: nil
    )
  5. Crear el objeto AptosConfig

    let aptosConfig = AptosConfig(settings: aptosSettings)
  6. Crear el objeto Aptos

    Este objeto se usa para interactuar con la blockchain de Aptos. Sirve como el punto de entrada para todas las interacciones con la blockchain.

    let aptos = Aptos(config: aptosConfig, graceFull: false)
  7. Obtener el ID de cadena

    let chainId = try await aptos.getChainId()

    ¡Felicidades! Has configurado exitosamente el SDK AptosKit y obtenido el ID de cadena de la blockchain de Aptos.

import SwiftUI
import AptosKit
struct ContentView: View {
@State private var chainId: String? = nil
var body: some View {
VStack {
if let chainId = chainId {
Text("ID de Cadena: \(chainId)")
} else {
Text("Obteniendo ID de Cadena...")
}
}
.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("Falló al obtener ID de cadena: \(error)")
self.chainId = "Error"
}
}
}
}
}