iOS

📱 Installation guide for our iOS SDK

💡

Supporting the latest versions

The SDK supports iOS 14 and above

Download

Download the SDK by adding the following repository to the Swift Package Manager: https://github.com/moonpay/mobile-sdk-ios.

Initialize

Initialize the SDK in your application with desired flow, environment and any parameters related to Buy, Sell, NFT or Swap.

import MoonPaySdk

// These run in your application and are all the of handlers available to you.
let handlers = MoonPayHandlers(
  onAuthToken: { data in
  	print("onAuthToken called", data)
	},
  onSwapsCustomerSetupComplete: {
    print("onSwapsCustomerSetupComplete called")
  },
  onUnsupportedRegion: {
    print("onUnsupportedRegion called")
  },
  onLogin: { data in
    print("onLogin called", data)
  },
  onInitiateDeposit: { data in
    print("onInitiateDepositCalled")
    let response = OnInitiateDepositResponsePayload(depositId: "yourDepositId")
    return reply
  }
)

let params = MoonPayBuyQueryParams(apiKey: "pk_test_123")
params.setBaseCurrencyCode(value: "GBP")
params.setBaseCurrencyAmount(value: 800)

let config = MoonPaySdkBuyConfig(
  debug: false,
  environment: MoonPayWidgetEnvironment.sandbox,
  params: params,
  handlers: handlers
)

let moonPaySdk = MoonPayiOSSdk(config: config)

moonPaySdk.show(mode: MoonPayRenderingOptioniOS.WebViewOverlay())

SDK Configuration

See the parameters pages for Buy, Sell, NFT and Swap for details on which parameters can be passed for each flow.

MoonPayiOSSdk

Arguments:

  • config: MoonPaySdkConfig,

MoonPaySdkConfig

Your config variant must match your query params variant.

Variants:

  • MoonPaySdkBuyConfig
  • MoonPaySdkSellConfig
  • MoonPaySdkSwapsCustomerSetupConfig
  • MoonPaySdkLegacyConfig

Arguments:

  • debug: Boolean
  • environment: MoonPayWidgetEnvironment
  • params: MoonPayQueryParams
  • handlers: MoonPayHandlers?

📘

Configuration

Possible values for environment: MoonPayWidgetEnvironment.sandbox, MoonPayWidgetEnvironment.production

MoonPayQueryParams

Your query params variant must match your config variant.

Variants:

  • MoonPayBuyQueryParams
  • MoonPaySellQueryParams
  • MoonPaySwapsCustomerSetupParams
  • MoonPayLegacyParams

MoonPayHandlers

All of the following are optional. However, you must explicitly pass nil for each handler you don't need to implement.

Arguments:

  • onSwapsCustomerSetupComplete: () -> Void
  • onUnsupportedRegion: () -> Void
  • onKmsWalletCreated: () -> Void
  • onAuthToken: (OnAuthTokenRequestPayload) -> Void
  • onLogin: (OnLoginRequestPayload) -> Void
  • onInitiateDeposit: (OnInitiateDepositRequestPayload) -> OnInitiateDepositResponsePayload

Handler Payload Types

OnAuthTokenRequestPayload

Arguments:

  • token: String
  • csrfToken: String

OnLoginRequestPayload

Arguments:

  • isRefresh: Boolean

OnInitiateDepositRequestPayload

Arguments:

  • transactionId: String
  • cryptoCurrencyAmount: String
  • cryptoCurrencyAmountSmallestDenomination: String
  • fiatCurrencyAmount: String?
  • depositWalletAddress: String
  • cryptoCurrency: CryptoCurrency
  • fiatCurrency: FiatCurrency

OnInitiateDepositResponsePayload

Arguments:

  • depositId: String

CryptoCurrency

Arguments:

  • id: String
  • name: String
  • code: String
  • contractAddress: String?
  • chainId: String?
  • coinType: String?
  • networkCode: String?

FiatCurrency

Arguments:

  • id: String
  • name: String
  • code: String

Display

If you're using the walletAddress or walletAddresses query param, you need to sign your widget URL before you can display the widget. Learn more about URL signing.

moonPaySdk.show takes one argument, mode, which takes a value of type MoonPayRenderingOptioniOS. The mode variants are MoonPayRenderingOptioniOS.WebViewOverlay() and MoonPayRenderingOptioniOS.InAppBrowser().

let moonpay = MoonPayiOSSdk(config: config)

let urlSignature = moonpay.generateUrlForSigning()
let encoder = JSONEncoder()

// The URL for signature should be sent to your backend, which should then
// sign it with your API secret and return the signature.

// Once you have the signature, you can update the SDK with it and show the
// widget.
if let json = try? encoder.encode(urlSignature) {
    do {
        let (data, response) = try await upload(data: json, url: '/sign-url')
        moonPaySdk.updateSignature(response)
    } catch {
        print("Something went wrong")
    }
}

func upload(_ data: Data, to url: URL) async throws -> URLResponse {
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    let (responseData, response) = try await session.upload(
        for: request, from: data
    )

    return response
}

moonPaySdk.show(mode: .WebViewOverlay())