Optimism
准备好在生产环境中调用了吗?
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Optimism
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
web3.swift is the primary Swift library for OP Mainnet, giving native iOS and macOS apps direct access to chain ID 10 (where ETH is the gas token). It targets iOS 13+ and macOS 10.15+, so it covers any modern Apple platform that supports async/await. Add it through Swift Package Manager using the repository at https://github.com/argentlabs/web3.swift. Because OP Mainnet is EVM-equivalent, the same calls work as on any other Ethereum-style chain.
web3.swift supports iOS 13+ and macOS 10.15+, matching the platforms where Swift's async/await is available. Add it to your project through Swift Package Manager by pointing at the argentlabs/web3.swift repository.
Create the client with Web3(rpcURL: "https://optimism.therpc.io/YOUR_API_KEY") — a single initializer that wires up the OP Mainnet connection. Every network call is async throws, so invoke it from an async context with try await, either inside a Task { } or from another async function. That keeps the UI responsive while a balance lookup or transaction round-trips to chain ID 10.
In SwiftUI, drive a live OP Mainnet balance through an ObservableObject view model held with @StateObject: publish the balance as a @Published property and the view redraws whenever it changes. Trigger the async fetch from a Button action wrapped in Task { }, or from the view's .task modifier so it runs when the view appears — either way the try await call stays off the main thread until its result is ready to publish.
To call a contract on OP Mainnet, load its ABI JSON and build a contract handle with web3.eth.Contract(json:address:). From there you invoke a named method with its parameters and await the .call() result — useful for reading state from a Superchain protocol like Velodrome or Synthetix without leaving Swift.
Define a typed EthereumError enum so your code can tell an invalid address apart from a genuine network failure on OP Mainnet — each case carries its own meaning and your UI can react accordingly. Validate the address format (for instance, that it starts with 0x) before making any network call, so an obvious typo fails fast and cheaply instead of round-tripping to chain ID 10 only to error out.