Portal's SDK for iOS.
Full documentation and Guides are available in the Portal Docs.
PortalSwift supports both Swift Package Manager and Cocoapods.
Depending on the needs of your project, you can follow the instructions below to install portal-ios.
To integrate PortalSwift into your Xcode project using Swift Package Manager, in XCode, select File -> Swift Packages -> Add Package Dependency and search for PortalSwift or enter the URL of this repository (https://github.com/portal-hq/PortalSwift).
This will add PortalSwift as a dependency in your project.
If you'd prefer, you can manually add PortalSwift to your project's Package.swift file:
dependencies: [
.package(url: "https://github.com/portal-hq/PortalSwift.git", .upToNextMajor(from: "2.0.9"))
]To integrate PortalSwift into your Xcode project using Cocoapods, add the following to your Podfile:
pod 'PortalSwift', :git => 'https://github.com/portal-hq/PortalSwift'Then run pod install.
To create a new instance of Portal within your app, import PortalSwift and initialize a new instance of Portal.
import PortalSwift
let portal = try Portal(
apiKey: "YOUR_PORTAL_CLIENT_API_KEY",
withRpcConfig: [
"eip155:1": "RPC_URL_FOR_ETHEREUM_MAINNET"
"eip155:11155111": "RPC_URL_FOR_ETHEREUM_SEPOLIA",
"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp": "RPC_URL_FOR_SOLANA_MAINNET",
"solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z": "RPC_URL_FOR_SOLANA_TESTNET",
],
autoApprove: true, // If you'd like to set up custom approval logic, you can leave this out
)For more info on the basic usage of the Portal class and its initialization parameters, see the Portal Docs.
To create a wallet using your new Portal instance, call the createWallet method. This function returns a tuple (ethereum, solana) containing the string literals for the addresses of your newly created wallets.
createWallet() also optionally accepts a progress handler which sends status updates on the wallet creation process.
let (ethereum, solana) = try await portal.createWallet() { createStatus in
print("Wallet Creation Status: ", createStatus)
}
print("Ethereum Address: ", ethereum)
print("Solana Address: ", solana)For more info on creating a wallet, see the Portal Docs.
To send a transaction using your new Portal instance, call the request(chainId, method, params) method.
chainIdis the CAIP-2 Blockchain ID of the network you'd like to send the transaction onwithMethodis the method you'd like to call on the network. This should be a member of thePortalRequestMethodsenumandParamsis an array of parameters required for the method you're calling- For transactions, this should be an array of dictionaries containing the transaction details
let transaction: [String: String] = [
"from": ethereum,
"to": "DESTINATION_ADDRESS",
"value": "HEX_ENCODED_VALUE_IN_WEI",
]
let signature = try await portal.request("eip155:11155111", withMethod: .eth_signTransaction, andParams: [transaction])For more info on signing a transaction, see the Portal Docs.
To send a transaction using your new Portal instance, call the request(method, params) method.
let transaction: [String: String] = [
"from": ethereum,
"to": "DESTINATION_ADDRESS",
"value": "HEX_ENCODED_VALUE_IN_WEI",
]
let transactionHash = try await portal.request("eip155:11155111", withMethod: .eth_sendTransaction, andParams: [transaction])To send a transaction using your new Portal instance, call the sendSol(lamports, address, chainId) method.
let transactionHash = try await portal.sendSol(1, to: "GPsPXxoQA51aTJJkNHtFDFYui5hN5UxcFPnheJEHa5Du", withChainId: chainId)By default, PortalSwift will register backup methods for Google Drive, iCloud, Passkeys, and Passwords. Google Drive require additional configuration to successfully back up your wallet.
To configure the Google Drive backup method, use the setGoogleDriveConfiguration(clientId).
clientIdis the OAuth Client ID for your Google Drive app.
try portal.setGoogleDriveConfiguration(clientId: "YOUR_GOOGLE_DRIVE_CLIENT_ID")To execute a backup, call the backupWallet(method) method.
methodis the backup method you'd like to use. This should be a member of the providedBackupMethodsenum.
This function returns a tuple (cipherText, storageCallback) containing all necessary information required to complete the backup process on your end.
cipherTextthis is the encrypted wallet data that you will need to store on your end for future wallet recoverystorageCallbackthis is a callback function that you will need to call once you've completed the storage process on your end
backupWallet() also optionally accepts a progress handler which sends status updates on the wallet backup process.
let (cipherText, storageCallback) = try await portal.backupWallet(.iCloud) { backupStatus in
print("Backup Status: ", backupStatus)
}
// Store the cipherText on your end
storeUserCipherText(cipherText) // This would be a function you define to store the cipherText
// Call the storageCallback once you've completed the storage process
try await storageCallback()Before calling backupWallet(.Password), you must set a password using the portal.setPassword(password) method.
try portal.setPassword(password: "YOUR_PASSWORD")
let (cipherText, storageCallback) = try await portal.backupWallet(.Password) { backupStatus in
print("Backup Status: ", backupSatus)
}
// Store the cipherText on your end
storeUserCipherText(cipherText) // This would be a function you define to store the cipherText
// Call the storageCallback once you've completed the storage process
try await storageCallback()Before calling backupWallet(.Passkey), you must set a Authentication Anchor using the portal.setPasskeyAuthenticationAnchor(anchor) method.
anchoris an instance conforming to theASPresentationAnchorprotocol. This will be used to present the passkey authentication view controller.
try portal.setPasskeyAuthenticationAnchor(anchor: self.view.window)
let (cipherText, storageCallback) = try await portal.backupWallet(.Passkey) { backupStatus in
print("Backup Status: ", backupStatus)
}
// Store the cipherText on your end
storeUserCipherText(cipherText) // This would be a function you define to store the cipherText
// Call the storageCallback once you've completed the storage process
try await storageCallback()To recover a wallet using your new Portal instance, call the recoverWallet(method, withCipherText) function.
methodis the backup method you used to backup your walletcipherTextis the encrypted wallet data you stored during the backup process
This function returns a tuple (ethereum, solana) containing the string literals for the addresses of your recovered wallets.
recoverWallet() also optionally accepts a progress handler which sends status updates on the wallet recovery process.
let (ethereum, solana) = try await portal.recoverWallet(.iCloud, withCipherText: cipherText) { recoveryStatus in
print("Recovery Status: ", recoveryStatus)
}
print("Ethereum Address: ", ethereum)
print("Solana Address: ", solana)Want to integrate Web3 into your app? Visit our site to learn more, or reach out to Portal to get a demo.