iOS 开发者使用 Kaptos 入门
本指南将带您完成 AptosKit 的设置,并获取 Aptos 区块链上的数据。
-
安装 SDK
AptosKit 以 Swift 包形式提供。要将其添加到项目,请在
Package.swift文件中添加以下内容:dependencies: [.package(url: "https://github.com/mcxross/swift-aptos.git", .upToNextMajor(from: <version>))] -
导入 SDK
在 Swift 文件中导入 SDK:
import AptosKit -
创建 ClientConfig 对象
此对象用于配置客户端行为。您可以设置
maxRetries、requestTimeout和retryOnServerErrors属性。let config = ClientConfig(followRedirects: true,agent: "AptosClient",likeAgent: nil,requestTimeout: 5000,retryOnServerErrors: 3,maxRetries: 5,cache: false,proxy: nil) -
创建 AptosSettings 对象
此对象用于配置 Aptos 网络连接。您可以设置
network、fullnode和faucet属性。let aptosSettings = AptosSettings(network: .devnet,fullNode: nil,faucet: nil,indexer: nil,client: nil,clientConfig: config,fullNodeConfig: nil,indexerConfig: nil,faucetConfig: nil) -
创建 AptosConfig 对象
let aptosConfig = AptosConfig(settings: aptosSettings) -
创建 Aptos 对象
此对象用于与 Aptos 区块链交互,是所有链上交互的入口点。
let aptos = Aptos(config: aptosConfig, graceFull: false) -
获取链 ID
let chainId = try await aptos.getChainId()恭喜!您已经成功设置 AptosKit SDK,并从 Aptos 区块链获取了链 ID。
import SwiftUIimport 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" } } } }}