Keyri QR Login - AlphaPoint iOS Apps

Install Keyri SDK

CocoaPods (opens in a new tab) is a dependency manager for Cocoa projects. To integrate the Keyri iOS SDK into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'keyri-pod'

The SDK can then be imported into any Swift file as follows:

import keyri-pod

Set Up User Login

Please refer to iOS documentation for detailed instructions about how to set up the SDK to handle Authentication via URL Deep linking and/or a built in Scanner

Provision and retrieve API login credentials from the APEX WebSocket API with GetAPICredentials()

Keyri's session object has a "payload" field in which you can input any data, which will be encrypted all the way to the browser when the user scans the login QR code and the valid session is confirmed. You can fetch the user's API credentials from the APEX user management system at any point before this, and populate this field with the API credentials before going on to confirm the session. Please see sample code below

Important Note: The APIKey and APISecret will be decommissioned as soon as the user is logged in on the browser.

This function can be activated once the login QR code has been scanned and parsed, either from a Universal Link (iOS) / App Link (Android) or your app's built-in QR code scanner.

Construct the function as follows:

  1. Send the following message to wss://apexapi.{yourDomain}/WSGateway with the user's UserId:
{
  "m": 0,
  "i": 76,
  "n": "AddUserAPIKey",
  "o": "{\"UserId\":{UserId},\"Permissions\":[\"Trading\",\"Withdraw\",\"Deposit\"]}"
}
  1. In return, you will receive an APIKey and APISecret as follows:
{
  "m": 1,
  "i": 76,
  "n": "AddUserAPIKey",
  "o": "{\"APIKey\":\"...\",\"APISecret\":\"...\",\"UserId\":[UserId],\"Permissions\":[\"Trading\",\"Withdraw\",\"Deposit\"],\"Nonce\":0}"
}
  1. Finally, the function must return values for APIKey, APISecret, and UserId. These three items will subsequently be passed into the Keyri initiateQrSession() method. Please see the iOS App Integration page for details on how this function is invoked

This example uses Native iOS functions to handle websockets

struct SocketMessage: Codable {
    let m: Int
    let i: Int
    let n: String
    let o: String
}
 
func getApiCredentials(completion: @escaping (String?) -> Void) {
    let urlSession = URLSession(configuration: .default)
    let webSocketTask = urlSession.webSocketTask(with: "wss://apexapi.{yourDomain}/WSGateway")
    webSocketTask.resume()
 
    let message = try? JSONDecoder().decode(SocketMessage.self, from: data)
    webSocketTask.send(message) { error in
        if let error = error {
            print("WebSocket couldn’t send message because: \(error)")
        }
    }
    webSocketTask.receive { result in
        switch result {
        case .failure(let error):
            print("Error in receiving message: \(error)")
        case .success(let message):
            switch message {
            case .string(let text):
                print("Received string: \(text)")
            case .data(let data):
                print("Received data: \(data)")
            }
        }
    }
}