iOS API Reference

iOS API Reference

#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;
magic.auth.loginWithSMS;
magic.auth.loginWithEmailOTP;

#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.

#loginWithSMS

Authenticate a user passwordlessly using a one-time code sent to the specified phone number.

List of Currently Blocked Country Codes

#Public Methods

Methods
loginWithSMS( _ configuration: LoginWithSMSConfiguration, response: (_ resp: Response<String>) -> Void )
loginWithSMS(_ configuration: LoginWithSMSConfiguration) -> 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 phoneNumberInput: UITextField!
    let magic = Magic.shared

    @IBAction func login() {
        guard let magic = magic else { return }
        guard let phoneNumber = self.phoneNumberInput.text else { return }

        let configuration = LoginWithSMSConfiguration(phoneNumber: phoneNumber)

        magic.auth.loginWithSMS(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 phoneNumberInput: UITextField!
    let magic = Magic.shared

    @IBAction func login() {
        guard let magic = magic else { return }

        let configuration = LoginWithSMSConfiguration(phoneNumber: phoneNumber)

        magic.auth.loginWithSMS(configuration).done({ result in
            print(result) // DIDToken
        }).catch({
            print(error) // handle Error
        })
    }
}

#Associated Class

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

  • phoneNumber The user phone number to log in with.

#loginWithEmailOTP

Authenticate a user passwordlessly using an email one-time code sent to the specified user's email address.

#Public Methods

Methods
loginWithEmailOTP( _ configuration: LoginWithEmailOTPConfiguration, response: (_ resp: Response<String>) -> Void )
loginWithEmailOTP(_ configuration: LoginWithEmailOTPConfiguration) -> 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 = LoginWithEmailOTPConfiguration(email: email)

        magic.auth.loginWithEmailOTP(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 = LoginWithEmailOTPConfiguration(email: self.emailInput.text!)

        magic.auth.loginWithEmailOTP(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.

#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)
        })
    }
}

Did you find what you were looking for?