API Reference | Magic Client SDK for iOS AppsAPI Reference | Magic Client SDK for iOS Apps

API Reference | Magic Client SDK for iOS AppsAPI Reference | Magic Client SDK for iOS Apps

Constructor

Configure and construct your Magic SDK instance.

Magic

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

import MagicSDK import UIKit @UIApplicationMain func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // assign the newly created Magic instance to shared property // Test key defaults to "rinkeby", live key defaults to "mainnet" Magic.shared = Magic("YOUR_PUBLISHABLE_API_KEY"); return true }

Auth Module

The Auth Module and its members are accessible on the Magic SDK instance by the auth property.

import MagicSDK let magic = Magic.shared magic.auth; magic.auth.loginWithMagicLink;

Authenticate a user passwordlessly using a "magic link" sent to the specified user's email address.

Public Methods
Methods
loginWithMagicLink( _ configuration: LoginWithMagicLinkConfiguration, response: (_ resp: Response<String>) -> Void )
loginWithMagicLink(_ configuration: LoginWithMagicLinkConfiguration) -> Promise <String>
Returns

Promise<string | null>: The promise resolves upon authentication request success and rejects with a specific error code if the request fails. The resolved value is a Decentralized ID token with a default 15-minute lifespan.

Example

Closure

import MagicSDK class LoginViewController: UIViewController { @IBOutlet weak var emailInput: UITextField! let magic = Magic.shared @IBAction func login() { guard let magic = magic else { return } guard let email = self.emailInput.text else { return } let configuration = LoginWithMagicLinkConfiguration(email: email) magic.auth.loginWithMagicLink(configuration, response: { response in guard let token = response.result else { return print("Error:", response.error.debugDescription) } print("Result", token) }) } }

Promise

import MagicSDK class LoginViewController: UIViewController { @IBOutlet weak var emailInput: UITextField! let magic = Magic.shared @IBAction func login() { guard let magic = magic else { return } let configuration = LoginWithMagicLinkConfiguration(email: self.emailInput.text!) magic.auth.loginWithMagicLink(configuration).done({ result in print(result) // DIDToken }).catch({ print(error) // handle Error }) } }
Associated Class

LoginWithMagicLinkConfiguration(showUI: Bool = true, email: String)

  • email The user email to log in with.
  • showUI If true, show an out-of-the-box pending UI while the request is in flight.

User Module

The User Module and its members are accessible on the Magic SDK instance by the user property.

import MagicSDK let magic = Magic.shared magic.user magic.user.getIdToken magic.user.generateIdToken magic.user.getMetadata magic.user.updateEmail magic.user.isLoggedIn magic.user.logout

updateEmail

Initiates the update email flow that allows a user to change to a new email

Public Methods
Methods
updateEmail( _ configuration: UpdateEmailConfiguration, response: (_ resp: Response<Bool>) -> Void )
updateEmail(_ configuration: UpdateEmailConfiguration) -> Promise <Bool>
Returns

Promise<Bool>: The promise resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails.

Example
import MagicSDK class MagicViewController: UIViewController { let magic = Magic.shared // Initiates the flow to update a user's current email to a new one. func updateEmail() { guard let magic = magic else { return } // Assuming user is logged in let configuration = UpdateEmailConfiguration(email: "new_user_email@example.com") magic.user.updateEmail(configuration, response: { response in guard let result = response.result else { return print("Error:", response.error.debugDescription) } print("Result", result) }) } }
Associated Class

UpdateEmailConfiguration(showUI: Bool = true, email: String)

  • email The user email to update with.
  • showUI If true, show an out-of-the-box pending UI while the request is in flight.

getIdToken

Generates a Decentralized Id Token which acts as a proof of authentication to resource servers.

Public Methods
Methods
getIdToken( _ configuration: GenerateIdTokenConfiguration? = nil, response: (_ resp: Response<String>) -> Void )
getIdToken(_ configuration: GenerateIdTokenConfiguration? = nil) -> Promise <String>
Returns

Promise<String>: Base64-encoded string representation of a JSON tuple representing [proof, claim]

Example
import MagicSDK class MagicViewController: UIViewController { let magic = Magic.shared func getIdToken() { guard let magic = magic else { return } // Assuming user is logged in let configuration = GetIdTokenConfiguration(lifespan: 900) magic.user.getIdToken(configuration, response: { response in guard let token = response.result else { return print(response.error.debugDescription) } print(token) }) } }
Associated Class

GetIdTokenConfiguration(lifespan: Int = 900)

  • lifespan? (Int): will set the lifespan of the generated token. Defaults to 900s (15 mins)

generateIdToken

Generates a Decentralized Id Token with optional serialized data.

Public Methods
Methods
generateIdToken( _ configuration: GenerateIdTokenConfiguration, response: (_ resp: Response<String>) -> Void )
generateIdToken(_ configuration: GenerateIdTokenConfiguration) -> Promise <String>
Returns

Promise<String>: Base64-encoded string representation of a JSON tuple representing [proof, claim]

Example
import MagicSDK class MagicViewController: UIViewController { let magic = Magic.shared func generateIdToken() { guard let magic = magic else { return } // Assuming user is logged in let configuration = GenerateIdTokenConfiguration(lifespan: 900, attachment: "none") magic.user.generateIdToken(configuration, response: { response in guard let token = response.result else { return print("Error:", response.error.debugDescription) } print("Result", token) }) } }
Associated Class

GenerateIdTokenConfiguration(lifespan: Int = 900, attachment: String = 'none')

  • lifespan (Number) : will set the lifespan of the generated token. Defaults to 900s (15 mins)
  • attachment (String) : will set a signature of serialized data in the generated token. Defaults to "none"

getMetadata

Retrieves information for the authenticated user.

Public Methods
Methods
getMetadata( response: (_ resp: Response<String>) -> Void )
getMetadata() -> Promise <String>
Returns

UserMetadata: containing the issuer, email and cryptographic public address of the authenticated user.

public struct UserMetadata: MagicResponse { public let issuer: String? public let publicAddress: String? public let email: String? }
  • issuer : The Decentralized ID of the user. In server-side use-cases, we recommend this value to be used as the user ID in your own tables.
  • email : Email address of the authenticated user.
  • publicAddress: The authenticated user's public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
Example
import MagicSDK class MagicViewController: UIViewController { let magic = Magic.shared func getMetadata() { guard let magic = magic else { return } // Assuming user is logged in magic.user.getMetadata(response: { response in guard let metadata = response.result else { return print("Error:", response.error.debugDescription) } print("Result", metadata) }) } }

isLoggedIn

Checks if a user is currently logged in to the Magic SDK.

Public Methods
Methods
isLoggedIn( response: (_ resp: Response<Bool>) -> Void)
isLoggedIn() -> Promise <Bool>
Returns

Promise<Bool>

Example
import MagicSDK class MagicViewController: UIViewController { let magic = Magic.shared func isLoggedIn() { guard let magic = magic else { return } magic.user.isLoggedIn(response: { response in guard let result = response.result else { return print("Error:", response.error.debugDescription) } print("Result", result) }) } }

logout

Logs out the currently authenticated Magic user

Public Methods
Methods
logout( response: (_ resp: Response<Bool>) -> Void )
logout() -> Promise <Bool>
Returns

Promise<Bool>

Example
import MagicSDK class MagicViewController: UIViewController { let magic = Magic.shared func logout() { guard let magic = magic else { return } // Assuming user is logged in magic.user.logout(response: { response in guard let result = response.result else { return print("Error:", response.error.debugDescription) } print("Result", result) }) } }
API Reference | Magic Client SDK for iOS AppsAPI Reference | Magic Client SDK for iOS Apps