iOS SDK
Installation
The Elixxir SDK requires the use of the Swift Package Manager—a tool for managing the distribution of Swift code. To use the SDK, add it to dependencies in your Package.swift
file.
dependencies: [
.package(url: "https://git.xx.network/elixxir/elixxir-dapps-sdk-swift")
]
Initialization
The instructions below describe how to initialize the client and connect to the network.
-
First, you will need to create a secure area to store client data. You can do this by creating a
PasswordStorage
for saving and loading your client storage. For example purposes we will use UserDefaults, but really encourage you to not use it and useKeychain
instead.let demoPasswordStorage = PasswordStorage(
save: { password in
UserDefaults.standard.set(password, forKey: "SDKPassword")
},
load: {
UserDefaults.standard.value(forKey: "SDKPassword") as! Data
}) -
Next, create a client storage to manage client data stored on disk.
let clientStorage = ClientStorage.live(passwordStorage: demoPasswordStorage)
-
If a client storage already exists, load it instead of creating a new one.
let client = Client
/*
//loadClient is a blocking call, do it on the background thread
//createClient is a blocking call, do it on the background thread
*/
if clientStorage.hasStoredClient() {
client = try? clientStorage.loadClient()
} else {
client = try? clientStorage.createClient()
} -
Next, create an identity. You will need to create a connection later.
/*
//Blocking call, do it on the background thread
*/
let myIdentity = try client.makeIdentity() -
Start the network follower.
let networkFollower = client.networkFollower
/*
//Blocking call, do it on the background thread
//Time is in millisecond
*/
try networkFollower.start(timeoutMS: 30_000) -
Finally, wait for the network to be connected.
/*
//Blocking call, do it on the background thread
//Time is in millisecond
*/
let isConnected = client.waitForNetwork(30_000)
guard isConnected else { /* try again */ }
Send Rest-like message
The instructions below describe how to use the restlike API to send requests and receive messages like a rest API.
-
First, create an E2E client.
/*
//Blocking call, do it on the background thread.
*/
let clientE2E = try ClientE2ELogin.live(with: client) -
Next, to initiate a connection with the remote, use an unauthenticated connection. (To get remote identity, please refer to this.)
/*
//Blocking call, do it on the background thread
// You need your remote identity.
*/
let connection = try client.connect(withAuthentication: false, recipientContact: REMOTE_IDENTITY, e2eId: clientE2E.getId()) -
Next, create a
RequestSender
.let restLikeRequestSender = RestlikeRequestSender.live(authenticated: false)
-
Create your request that you will send.
/*
//All fields are customizable to meet your remote configuration
//Content accepts any json encoded object, this is where you include your request body
//Method number is used to refer to get, post, put, etc.
*/
let request = RestlikeMessage(
version: Int,
headers: YOUR_HEADERS,
content: YOUR_JSON_ENCODED_MODEL,
method: Int,
Uri: String,
error: String) -
Send your request and wait for a response.
/*
//Blocking call, do it on the background thread
//Response is RestLikeMessage object
*/
let response = try? restLikeRequestSender.send(
client.getId(),
connection!.getId(),
request)
/*
//Jumping to the main thread to update any UI with the response
*/
DispatchQueue.main.async {
let message = response
//Use the response message here to update UI
}
Send E2E message
-
First, create an E2E client.
/*
//Blocking call, do it on the background thread.
*/
let clientE2E = try ClientE2ELogin.live(with: client) -
Next, to initiate a connection with the remote, use an unauthenticated connection. (To get remote identity, please refer to this.)
/*
// Blocking call, do it on the background thread
// You need your remote identity.
*/
let connection = try client.connect(
withAuthentication: false,
recipientContact: REMOTE_IDENTITY,
e2eId: clientE2E.getId()) -
Setup your messages listener.
/*
//Blocking call, do it on the background thread
//messageType value must be same as the value you will use to send a message
*/
connection.listen(messageType: Int) { message in
/*
//Jumping to the main thread to update any UI with the message
*/
DispatchQueue.main.async {
let msg = message
//Use the received message here to update UI
}
} -
Send the message and wait to get the send report, which contains the delivery status.
/*
//Blocking call, do it on the background thread
//messageType value must be same as the value you used to setup your messages listener
*/
let sendReport = try connection?.send(
messageType: Int,
payload: YOUR_JSON_ENCODED_MODEL) -
Wait for the message to be delivered and update the UI if needed.
/*
//Blocking call, do it on the background thread
//Time is in millisecond
*/
client.waitForDelivery(report: sendReport, timeoutMS: 30_000) { result in
/*
//Jumping to the main thread to update any UI
*/
DispatchQueue.main.async {
switch result {
case .delivered(let roundResults):
// You can stop loading indicator here for example
case .notDelivered:
//Show error popup here for example
}
}
}