Use MetaMask SDK with iOS | MetaMask developer documentation (2024)

Import MetaMask SDK into your native iOS dapp to enable yourusers 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 deviceor emulator).You can install MetaMask Mobile from the App Storeor clone and compile MetaMask Mobile from sourceand 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

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 callingeth_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 callingeth_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 callingeth_sendTransaction.

  • Use a dictionary
  • Use a struct

If your request parameters make up a simple dictionary of string key-value pairs, you can use thedictionary 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 theiOS SDK GitHub repository for more information.

Use MetaMask SDK with iOS | MetaMask developer documentation (2024)

FAQs

How to use MetaMask SDK? ›

Steps​
  1. Install the SDK​ In your project directory, install the SDK using Yarn or npm: yarn add @metamask/sdk. ...
  2. Import the SDK​ In your project script, add the following to import the SDK: index.js. ...
  3. Instantiate the SDK​ Instantiate the SDK using any options: ...
  4. Use the SDK​ Use the SDK by calling any provider API methods.

How to use MetaMask in iOS? ›

Use MetaMask SDK with iOS
  1. Install the SDK​ To add the SDK as a Swift Package Manager (SPM) package to your project, in Xcode, select File > Swift Packages > Add Package Dependency. ...
  2. Import the SDK​ Import the SDK by adding the following line to the top of your project file: ...
  3. Connect your dapp​ ...
  4. Call provider methods​

What is SDK connections in MetaMask? ›

MetaMask SDK is a library that provides a reliable, secure, and seamless connection from your dapp to the MetaMask browser extension and MetaMask Mobile. You can install the SDK in existing dapps, and call any Wallet API methods from your dapp.

How do I enable developer mode in MetaMask? ›

Go to chrome://extensions . Toggle Developer mode on in the top right corner.

How do I use MetaMask step by step? ›

Input MetaMask into the search bar.
  1. Either click 'Get' or tap on the name of the app to see more details. Complete the usual App Store download confirmation process.
  2. Once installed, open the app and follow the prompts to create your wallet.
  3. Back up your Secret Recovery Phrase somewhere safe and offline!
May 21, 2024

What API does MetaMask use? ›

JSON-RPC API​ MetaMask uses the request() method of the provider API to wrap a JSON-RPC API. The JSON-RPC API contains standard Ethereum JSON-RPC API methods and MetaMask-specific methods. The RPC methods are documented in the interactive JSON-RPC API reference.

How safe is MetaMask on iOS? ›

MetaMask is considered to be a very safe crypto wallet. The team is well-respected and the platform is widely used. Their browser wallet has over 10 million users on the Chrome web store while the iPhone and Android mobile apps have over 40k and 260k user ratings, respectively.

How to download MetaMask extension for iOS? ›

Click "Install MetaMask for Chrome" or select another one of the supported browsers. Then click on "add to chrome" and then "add extension.

Does MetaMask work on Chrome for iOS? ›

MetaMask extension works fine with browsers such as Chrome, Opera, Edge, Firefox, and Brave. However, an iPad does not have any of the mentioned browsers. Hence, you cannot use the MetaMask extension on an iPad.

How does the SDK work? ›

A software development kit (SDK) is a set of platform-specific building tools for developers. You require components like debuggers, compilers, and libraries to create code that runs on a specific platform, operating system, or programming language. SDKs put everything you need to develop and run software in one place.

How does SDK integration work? ›

SDK integration is the process of importing a library file, known as a software development kit, into your project in order to use that file's functions.

How do I clear MetaMask SDK connections? ›

To close connections manually from MetaMask Mobile, go to Settings > Experimental, and select Clear MetaMask SDK connections.

How to open developer mode in iOS? ›

On the iOS device, open Settings > Privacy & Security > Developer Mode. Enable the toggle. You will receive a prompt from iOS to restart your device.

How do I get into developer mode? ›

How to access developer options on your Android phone
  1. Step 1: Go to Settings > About phone.
  2. Step 2: Scroll down to Build number.
  3. Step 3: Tap Build number seven times. ...
  4. Step 4: Once developer options are activated, you will see a message that reads, You are now a developer.
Mar 15, 2024

How to use MetaMask on mobile? ›

How to Set up your MetaMask Wallet (Mobile)
  1. How to Set up your MetaMask Wallet (Mobile)
  2. Download the MetaMask app on IOS or Android. Hit “Get Started.” Follow the steps to either Create a Wallet or Import a Wallet.
  3. Here, you can purchase Ether. ...
  4. Connect your wallet. ...
  5. Share your muerto on social media!

What is the communication layer of MetaMask SDK? ›

Communication layer​

The SDK uses elliptic curve integrated encryption scheme (ECIES) to communicate with MetaMask Mobile. ECIES is a hybrid encryption scheme that combines the benefits of both symmetric and asymmetric encryption.

How to install MetaMask plugin? ›

Click “Download Now”, and then click “o*k” on the pop up that appears.
  1. Type “about:addons” into browser URL bar.
  2. Click the Settings button > Click “Install Add-on from file”
  3. Browse for the zip or directory with MetaMask you downloaded.
  4. Click "Open"
  5. Click "Add" on the pop-up.
May 21, 2024

How do I use MetaMask for authentication? ›

Authentication Flow
  1. Connect to MetaMask and receive the user's address.
  2. Obtain a one-time code (nonce) for a user address.
  3. Sign a message containing nonce with a private key using MetaMask.
  4. Authenticate the user by validating the user's signature on the back end.
Sep 12, 2023

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6133

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.