Skip to main content

Use MetaMask SDK with iOS

Import MetaMask SDK into your native iOS dapp to enable your users to easily connect with their MetaMask Mobile wallet.

Prerequisites

  • MetaMask Mobile version 7.6.0 or later installed on your target device (that is, a physical device or emulator). You can install MetaMask Mobile from the App Store or clone and compile MetaMask Mobile from source and build to your target device.

  • iOS version 14 or later. The SDK supports ios-arm64 (iOS devices) and ios-arm64-simulator (M1 chip simulators). It currently doesn't support ios-ax86_64-simulator (Intel chip simulators).

Steps

1. Install the SDK

To add the SDK as a CocoaPods dependency to your project, add the following entry to our Podfile:

pod "metamask-ios-sdk"

Run the following command:

pod install

2. Import the SDK

Import the SDK by adding the following line to the top of your project file:

import metamask_ios_sdk

3. Connect your dapp

Connect your dapp to MetaMask by adding the following code to your project file:

let appMetadata = AppMetadata(name: "Dub Dapp", url: "https://dubdapp.com")

@ObservedObject var metamaskSDK = MetaMaskSDK.shared(appMetadata)

metamaskSDK.connect()

By default, MetaMask logs three SDK events: connectionRequest, connected, and disconnected. This allows MetaMask to monitor any SDK connection issues. To disable this, set MetaMaskSDK.shared.enableDebug = false or ethereum.enableDebug = false.

4. Call methods

You can now call any JSON-RPC API method using metamaskSDK.request().

Example: Get chain ID

The following example gets the user's chain ID by calling eth_chainId.

let chainIdRequest = EthereumRequest(method: .ethChainId)
let chainId = await metamaskSDK.request(chainIdRequest)

Example: Get account balance

The following example gets the user's account balance by calling eth_getBalance.

// Create parameters.
let account = metamaskSDK.account

let parameters: [String] = [
// Account to check for balance.
account,
// "latest", "earliest", or "pending" (optional)
"latest"
]

// Create request.
let getBalanceRequest = EthereumRequest(
method: .ethGetBalance,
params: parameters
)

// Make request.
let accountBalance = await metamaskSDK.request(getBalanceRequest)

Example: Send transaction

The following example sends a transaction by calling eth_sendTransaction.

If your request parameters make up a simple dictionary of string key-value pairs, you can use the dictionary directly. Note that Any or even AnyHashable types aren't supported, since the type must be explicitly known.

// Create parameters.
let account = metamaskSDK.account

let parameters: [String: String] = [
// Receiver address.
"to": "0x...",
// Sender address.
"from": account,
// Amount to send.
"value": "0x..."
]

// Create request.
let transactionRequest = EthereumRequest(
method: .ethSendTransaction,
// eth_sendTransaction expects an array parameters object.
params: [parameters]
)

// Make a transaction request.
let transactionResult = await metamaskSDK.request(transactionRequest)

Example

See the example iOS dapp in the iOS SDK GitHub repository for more information.