Skip to main content

Embedded Wallets SDK for iOS

Overview

MetaMask Embedded Wallets SDK (formerly Web3Auth Plug and Play/ W3A PnP) provides a seamless authentication experience for iOS applications with social logins, external wallets, and more. Using our iOS SDK written in Swift, you can easily connect users to their preferred wallets and manage authentication state natively.

Requirements

  • iOS 14+
  • Xcode 12+
  • Swift 5.x
  • Basic knowledge of Swift and iOS Development

Installation

Install the Web3Auth iOS SDK using one of the following methods:

Swift Package Manager

  1. In Xcode, with your app project open, navigate to File > Add Package Dependencies.

  2. When prompted, add the Web3Auth iOS SDK repository:

    https://github.com/Web3Auth/web3auth-swift-sdk

    From the Dependency Rule dropdown, select Exact Version and enter 11.1.0 as the version.

  3. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.

Cocoapods

To install the Web3Auth SDK using Cocoapods, follow the below steps:

  1. Open the Podfile, and add the Web3Auth pod:
pod 'Web3Auth', '~> 11.1.0'
  1. Once added, use pod install command to download the Web3Auth dependency.

Setup

info

Prerequisites Before you start, make sure you have registered on the Web3Auth Dashboard and have set up your project. You can look into the Dashboard Setup guide to learn more.

Configure Redirection

To use Web3Auth for iOS you need to Whitelist your bundleId in your Web3Auth project.

  • Go to Web3Auth Developer Dashboard, and create or open an existing Web3Auth project.
  • Whitelist {bundleId}://auth in the developer dashboard. This step is mandatory for the redirect to work.

Initialize Web3Auth

1. Create a Web3Auth Instance

Import and configure Web3Auth in your application:

import Web3Auth

class ViewController: UIViewController {
var web3Auth: Web3Auth?

override func viewDidLoad() {
super.viewDidLoad()

// Configure Web3Auth
web3Auth = Web3Auth(W3AInitParams(
clientId: "YOUR_CLIENT_ID", // Get your Client ID from Web3Auth Dashboard
network: .sapphire_mainnet, // or .sapphire_devnet
redirectUrl: "com.yourapp.bundleid://auth"
))
}
}

2. Initialize the Web3Auth Instance

Initialize the Web3Auth instance:

override func viewDidLoad() {
super.viewDidLoad()

Task {
do {
try await web3Auth?.initialize()
print("Web3Auth initialized successfully")
} catch {
print("Error initializing Web3Auth: \(error)")
}
}
}

3. Handle URL Callbacks

Configure your application to handle URL callbacks in your SceneDelegate:

import Web3Auth

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
Web3Auth().setResultUrl(url: url)
}

For applications using AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
Web3Auth().setResultUrl(url: url)
return true
}

Advanced Configuration

The Web3Auth iOS SDK offers a rich set of advanced configuration options:

tip

Head over to the advanced configuration sections to learn more about each configuration option.

web3Auth = Web3Auth(W3AInitParams(
clientId: "YOUR_CLIENT_ID",
network: .sapphire_mainnet, // or .sapphire_devnet
redirectUrl: "com.yourapp.bundleid://auth"
))

Blockchain Integration

Web3Auth is blockchain agnostic, enabling integration with any blockchain network. Out of the box, Web3Auth offers robust support for both Solana and Ethereum.

Ethereum Integration

For Ethereum integration, you can get the private key and use it with web3.swift or other Ethereum libraries:

import web3
import Web3Auth

// Use your Web3Auth instance to get the private key
val privateKey = web3Auth.getPrivKey()

// Generate the Ethereum Account
let account = try EthereumAccount(privateKey)

// Get the address
let address = account.address

// Create a client
let client = EthereumHttpClient(
// Please avoid using public RPC URL in production, use services
// like Infura, Quicknode, etc.
url: URL.init(string: rpcUrl)!,
// Replace with the chain id of the network you want to connect to
network: .custom(chainId)
)

// Get the balance
let balanceResponse = try await client.eth_getBalance(
// Use the address from previous step
address: address,
block: .Latest
)

// Convert the balance from Wei to Ether format
let balance = toEther(wei: balanceResponse)

Solana Integration

For Solana integration, you can get the Ed25519 private key:

import SolanaSwift

// Use your Web3Auth instance to get the private key
let ed25519PrivateKey = web3Auth.getEd25519PrivKey()

// Generate the KeyPair
let keyPair = try KeyPair(secretKey: Data(hex: ed25519PrivateKey))

// Get the user account
let userAccount = keyPair.publicKey.base58EncodedString

// Create the JSONRPCAPIClient instance
let endpoint = APIEndPoint(
address: "https://api.devnet.solana.com",
network: .devnet
)
let solanaJSONRPCClient = JSONRPCAPIClient(endpoint: endpoint)

// Get the balance
let balanceResponse = try await solanaJSONRPCClient.getBalance(
// Use userAccount from above
account: userAccount
)

// We are dividing the balance by 10^9, because Solana's token decimals is set to be 9;
let userBalance = return balanceResponse.convertToBalance(decimals: 9)