iOS SDK API

iOS SDK API

#Version

This documentation centers around using magic-ios SDK version 7.0.0 and above. You can view the open-source repository for this SDK on Github.

#Demo

Check out this repository for a demo application using of all of the following methods.

#Configuration & Setup

In order to use the Magic iOS SDK, you must get a valid publishable API key from the Magic Dashboard. In the examples below, replace the value of YOUR_API_KEY with the value you get from the dashboard. See the quickstart section for more details and installation instructions.

#Constructor

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.

Public constructors

Magic(apiKey: String)

Construct a Magic instance with publishable API Key retrieved from the Magic Dashboard

Magic(apiKey: String, network: EthNetwork)

Construct a Magic instance with publishable Key and Ethereum network

Magic(apiKey: String, customNode: CustomNodeConfiguration)

Construct a Magic instance with publishable Key and Custom Node configuration

#Example

In AppDelegate

Swift
01import MagicSDK
02import UIKit
03
04@UIApplicationMain
05func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
06
07    // assign the newly created Magic instance to shared property
08    // Test key defaults to "rinkeby", live key defaults to "mainnet"
09    Magic.shared = Magic("YOUR_PUBLISHABLE_API_KEY");
10
11    return true
12}

#Wallet Module Methods

All methods below are namespaced to the magic.wallet module. ⁠

#connectWithUI()

Request the user to connect their wallet to the dapp. This is done by using the EVM RPC method getAccount

#Arguments

None.

#Returns

  1. An string array ([String]) of user accounts that are connected, with the first element being the current public address of the user.

#Example

Swift
01guard let magic = magic else { return }
02
03magic.wallet.connectWithUI(response: { res in
04      if (res.status.isSuccess) {
05             print(res.result ?? "nil")
06                
07             let defaults = UserDefaults.standard
08             if let publicAddress = res.result?.first {
09                defaults.set(publicAddress, forKey: "publicAddress")
10                self.navigateToMain()
11             }
12     }
13})

#⁠getInfo()

Get information about the wallet the user is currently logged-in with. User must be signed in for this method to return or else it will throw an error. 

#Arguments

None.

#Returns

  1. Returns a response with the wallet type as a String. ⁠Wallet type values are magicmetamaskcoinbase_wallet, or wallet_connect.

    Will throw an error if no user is logged in.

#Example

Swift
01guard let magic = magic else { return }
02
03⁠magic.wallet.getInfo(response: { response in
04   if (response.error != nil) {
05      print(response.error.debugDescription)
06   }
07   print("Wallet Type: \(response.result?.walletType ?? "No Wallet Type Found")")
08})

#showUI()

Displays the wallet widget. This is only supported for users who login with email or Google and not third party wallets such as metamask. User must be signed in for this method to return or else it will throw an error.

#Arguments

None.

#Returns

  1. Promise which resolves when the user closes the wallet widget window.

#Example

Swift
01guard let magic = magic else { return }
02
03magic.wallet.showUI(response: { response in
04  if (response.error != nil) {
05    print(response.error.debugDescription)
06  }
07  print(response.result?.description ?? "")
08})

#requestUserInfoWithUI()

Displays the wallet widget within an iframe that prompts the user to consent to sharing information with the requesting dApp with OpenID profile scopes. Currently, the only profile scope that can be requested is a verified email. Collecting a verified email address from third-party wallet users (MetaMask, Coinbase Wallet, etc.) is a premium feature but included in the free trial period (see pricing). User must be signed in for this method to return or else it will throw an error.

#Arguments

Parameter

Type

Definition

configuration:

RequestUserInfoWithUIConfiguration?

scope (WalletUserInfoScope): When email: required | optional provided the user will be required (or not) to complete the request.

#Returns

  1. Returns a promise which will resolve to an object containing the requested information or rejects if the user declines to share information.

#Example

Swift
01
02
03guard let magic = magic else { return }
04
05magic.wallet.requestUserInfoWithUI(response: { response in
06    if (response.error != nil) {
07       print(response.error.debugDescription)
08    }
09    print("Email: \(response.result?.email ?? "No Email Found")")
10})
11

#disconnect()

Disconnects the user from the dApp. This is similar to logging a user out in that it will clear the authenticated state for the current user, the iframe, and any third party wallet the user may have signed in with. User must be signed in for this method to return or else it will throw an error.

#Arguments

None.

#Return Value

  1. promise that return a boolean: If the user's session was succesfully cleared or not.

#Example

Swift
01
02guard let magic = magic else { return }
03        
04magic.wallet.disconnect(response: { response in
05  print(response.result?.description ?? "")
06})
07

Did you find what you were looking for?