Skip to main content

Installation

To interact with the Ethereum blockchain, Magic iOS SDK embeds Web3.swift as a sub-dependency. No more extra dependency is needed.

Initialization

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.
NoteFollowing example is using Swift 5 with Xcode 11. iOS demo will be open-sourced soon.
Swift
Swift

Use Different Networks

Testnet

Goerli Block Explorer: https://goerli.etherscan.io Goerli Testnet Faucet: https://goerlifaucet.com
Swift

Custom Node

You can allow specific URLs to interact with the Magic SDK, such as a custom RPC URL to send transactions to your node. The Content Security Policy (CSP) of a browser dictates what resources can be loaded. You can update the policy in the settings page of the dashboard with your custom URL.
NoteNote: the use of a custom node will require the RPC URL to the project’s Content Security Policy from your Magic Dashboard. Refer to the CSP documentation.
Swift
ImportantDo not set the custom nodes to local IP address (E.x. “http://127.0.0.1”). Local IP will point to the network environment inside mobile device / simulator

Associated Class

CustomNodeConfiguration(rpcUrl: String, chainId: Int?)
  • rpcUrl :Your own node URL
  • chainId : Your own node’s chainId
EthNetwork
Swift

Common Methods

Send Transaction

Swift

Sign Message

Magic iOS SDK extends the functionality from Web3.swift to allow developers to sign Typed Data. Make sure to import MagicSDK while using these functions.

Eth Sign

Swift

Sign Typed Data Legacy (V1)

Swift

Sign Typed Data v3(EIP 712)

Swift

Sign Typed Data V4 (EIP 712)

Swift

Get User Info

Swift

Smart Contract

In this example, we’ll be demonstrating how to use Magic with Web3.swift to interact with Solidity smart contracts. The simple Hello World contract allows anyone to read and write a message to it.
Solidity

Deploy Contract

Swift

Read From Contract

Swift

Write to Contract

Swift

Resources