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

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}

#Auth Module

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

Swift
01import MagicSDK
02
03let magic = Magic.shared
04
05magic.auth;
06magic.auth.loginWithMagicLink;
07magic.auth.loginWithSMS;
08magic.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

Swift
01import MagicSDK
02
03class LoginViewController: UIViewController {
04
05    @IBOutlet weak var emailInput: UITextField!
06    let magic = Magic.shared
07
08    @IBAction func login() {
09        guard let magic = magic else { return }
10        guard let email = self.emailInput.text else { return }
11
12        let configuration = LoginWithMagicLinkConfiguration(email: email)
13
14        magic.auth.loginWithMagicLink(configuration, response: { response in
15            guard let token = response.result
16                else { return print("Error:", response.error.debugDescription) }
17            print("Result", token)
18        })
19    }
20}

Promise

Swift
01import MagicSDK
02
03class LoginViewController: UIViewController {
04
05    @IBOutlet weak var emailInput: UITextField!
06    let magic = Magic.shared
07
08    @IBAction func login() {
09        guard let magic = magic else { return }
10
11        let configuration = LoginWithMagicLinkConfiguration(email: self.emailInput.text!)
12
13        magic.auth.loginWithMagicLink(configuration).done({ result in
14            print(result) // DIDToken
15        }).catch({
16            print(error) // handle Error
17        })
18    }
19}

#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

Swift
01import MagicSDK
02
03class LoginViewController: UIViewController {
04
05    @IBOutlet weak var phoneNumberInput: UITextField!
06    let magic = Magic.shared
07
08    @IBAction func login() {
09        guard let magic = magic else { return }
10        guard let phoneNumber = self.phoneNumberInput.text else { return }
11
12        let configuration = LoginWithSMSConfiguration(phoneNumber: phoneNumber)
13
14        magic.auth.loginWithSMS(configuration, response: { response in
15            guard let token = response.result
16                else { return print("Error:", response.error.debugDescription) }
17            print("Result", token)
18        })
19    }
20}

Promise

Swift
01import MagicSDK
02
03class LoginViewController: UIViewController {
04
05    @IBOutlet weak var phoneNumberInput: UITextField!
06    let magic = Magic.shared
07
08    @IBAction func login() {
09        guard let magic = magic else { return }
10
11        let configuration = LoginWithSMSConfiguration(phoneNumber: phoneNumber)
12
13        magic.auth.loginWithSMS(configuration).done({ result in
14            print(result) // DIDToken
15        }).catch({
16            print(error) // handle Error
17        })
18    }
19}

#Associated Class

LoginWithSMSConfiguration(phoneNumber: 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

Swift
01import MagicSDK
02
03class LoginViewController: UIViewController {
04
05    @IBOutlet weak var emailInput: UITextField!
06    let magic = Magic.shared
07
08    @IBAction func login() {
09        guard let magic = magic else { return }
10        guard let email = self.emailInput.text else { return }
11
12        let configuration = LoginWithEmailOTPConfiguration(email: email)
13
14        magic.auth.loginWithEmailOTP(configuration, response: { response in
15            guard let token = response.result
16                else { return print("Error:", response.error.debugDescription) }
17            print("Result", token)
18        })
19    }
20}

Promise

Swift
01import MagicSDK
02
03class LoginViewController: UIViewController {
04
05    @IBOutlet weak var emailInput: UITextField!
06    let magic = Magic.shared
07
08    @IBAction func login() {
09        guard let magic = magic else { return }
10
11        let configuration = LoginWithEmailOTPConfiguration(email: self.emailInput.text!)
12
13        magic.auth.loginWithEmailOTP(configuration).done({ result in
14            print(result) // DIDToken
15        }).catch({
16            print(error) // handle Error
17        })
18    }
19}

#Associated Class

LoginWithEmailOTPConfiguration(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.

Typescript
01import MagicSDK
02
03let magic = Magic.shared
04
05magic.user
06magic.user.getIdToken
07magic.user.generateIdToken
08magic.user.getMetadata
09magic.user.updateEmail
10magic.user.isLoggedIn
11magic.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

Swift
01import MagicSDK
02
03class MagicViewController: UIViewController {
04
05    let magic = Magic.shared
06
07    // Initiates the flow to update a user's current email to a new one.
08    func updateEmail() {
09
10        guard let magic = magic else { return }
11
12        // Assuming user is logged in
13        let configuration = UpdateEmailConfiguration(email: "new_user_email@example.com")
14        magic.user.updateEmail(configuration, response: { response in
15            guard let result = response.result
16                else { return print("Error:", response.error.debugDescription) }
17            print("Result", result)
18        })
19    }
20}

#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

Swift
01import MagicSDK
02
03class MagicViewController: UIViewController {
04
05    let magic = Magic.shared
06
07    func getIdToken() {
08        guard let magic = magic else { return }
09
10        // Assuming user is logged in
11        let configuration = GetIdTokenConfiguration(lifespan: 900)
12
13        magic.user.getIdToken(configuration, response: { response in
14            guard let token = response.result
15                else { return print(response.error.debugDescription) }
16            print(token)
17        })
18    }
19}

#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

Swift
01import MagicSDK
02
03class MagicViewController: UIViewController {
04
05    let magic = Magic.shared
06
07    func generateIdToken() {
08        guard let magic = magic else { return }
09
10        // Assuming user is logged in
11        let configuration = GenerateIdTokenConfiguration(lifespan: 900, attachment: "none")
12
13        magic.user.generateIdToken(configuration, response: { response in
14            guard let token = response.result
15                else { return print("Error:", response.error.debugDescription) }
16            print("Result", token)
17        })
18    }
19}

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

Swift
01public struct UserMetadata: MagicResponse {
02    public let issuer: String?
03    public let publicAddress: String?
04    public let email: String?
05}
  • 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

Swift
01import MagicSDK
02
03class MagicViewController: UIViewController {
04
05    let magic = Magic.shared
06
07    func getMetadata() {
08        guard let magic = magic else { return }
09
10        // Assuming user is logged in
11        magic.user.getMetadata(response: { response in
12            guard let metadata = response.result
13                else { return print("Error:", response.error.debugDescription) }
14            print("Result", metadata)
15        })
16    }
17}

#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

Swift
01import MagicSDK
02
03class MagicViewController: UIViewController {
04
05    let magic = Magic.shared
06
07    func isLoggedIn() {
08        guard let magic = magic else { return }
09
10        magic.user.isLoggedIn(response: { response in
11            guard let result = response.result
12                else { return print("Error:", response.error.debugDescription) }
13            print("Result", result)
14        })
15    }
16}

#logout

Logs out the currently authenticated Magic user

#Public Methods

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

#Returns

Promise<Bool>

#Example

Swift
01import MagicSDK
02
03class MagicViewController: UIViewController {
04
05    let magic = Magic.shared
06
07    func logout() {
08        guard let magic = magic else { return }
09
10        // Assuming user is logged in
11        magic.user.logout(response: { response in
12            guard let result = response.result
13                else { return print("Error:", response.error.debugDescription) }
14            print("Result", result)
15        })
16    }
17}

Did you find what you were looking for?