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.
-
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>))] -
Importar el SDK
Importa el SDK en tu archivo Swift:
import AptosKit -
Crear el objeto ClientConfig
Este objeto se usa para configurar el comportamiento del cliente. Puedes establecer propiedades
maxRetries
,requestTimeout
, yretryOnServerErrors
.let config = ClientConfig(followRedirects: true,agent: "AptosClient",likeAgent: nil,requestTimeout: 5000,retryOnServerErrors: 3,maxRetries: 5,cache: false,proxy: nil) -
Crear el objeto AptosSettings
Este objeto se usa para configurar la conexión de red de Aptos. Puedes establecer propiedades
network
,fullnode
, yfaucet
.let aptosSettings = AptosSettings(network: .devnet,fullNode: nil,faucet: nil,indexer: nil,client: nil,clientConfig: config,fullNodeConfig: nil,indexerConfig: nil,faucetConfig: nil) -
Crear el objeto AptosConfig
let aptosConfig = AptosConfig(settings: aptosSettings) -
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) -
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.
Ejemplo Completo
Sección titulada «Ejemplo Completo»import SwiftUIimport 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" } } } }}