Polygon
¿Listo para usar esto en producción?
El plan gratuito cubre proyectos personales. El pago por uso escala sin necesidad de tarjeta.
Polygon
El plan gratuito cubre proyectos personales. El pago por uso escala sin necesidad de tarjeta.
web3.swift is the primary Swift library for building Polygon apps on Apple platforms. It targets iOS 13+ and macOS 10.15+, and because Polygon PoS is EVM-equivalent, its standard Ethereum interface connects to https://polygon.therpc.io/YOUR_API_KEY without any chain-specific changes. Add it to your project through Swift Package Manager using the argentlabs/web3.swift repository.
web3.swift targets iOS 13+ and macOS 10.15+, so it covers the platforms most consumer Polygon wallets and dApps ship on. Add it through Swift Package Manager using the argentlabs/web3.swift repository.
Create the client with Web3(rpcURL: "https://polygon.therpc.io/YOUR_API_KEY") and reuse it across your app. Every network call is async throws, so invoke it from an async context — inside a Task { }, a .task modifier, or another async function — and handle thrown errors with try. Balances come back in wei; convert them with .converted(to: .ether) to show MATIC amounts.
In SwiftUI, back your view with an ObservableObject view model and hold it as a @StateObject, publishing the balance with @Published so the UI re-renders automatically when a fresh value arrives from Polygon. Trigger the async fetches from a Task { } inside a Button action or from a .task modifier when the view appears — never block the main actor waiting on a chain-137 call.
To call a deployed Polygon contract, load its ABI JSON and the contract address through web3.eth.Contract(json:address:), then invoke its methods by name. The library decodes return values into Swift types, so reading on-chain state from chain 137 stays type-safe instead of dealing with raw ABI-encoded bytes.
Define a typed EthereumError enum so your app can distinguish an invalid address from a network failure or insufficient funds, and react to each case appropriately in the UI. Validate the address format up front — checking the 0x prefix and shape before making any call — so you fail fast on local input mistakes instead of wasting a round trip to the Polygon endpoint on chain 137.