The Elixxir SDK requires the use of the [Swift Package Manager](https://swift.org/package-manager/)—a tool for managing the distribution of Swift code. To use the SDK, add it to dependencies in your `Package.swift` file.
The instructions below describe how to initialize the client and connect to the network.
1. 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 use `Keychain` instead.
3. If a client storage already exists, load it instead of creating a new one.
```swift
letclient=Client
/*
//loadClient is a blocking call, do it on the background thread
//createClient is a blocking call, do it on the background thread
*/
ifclientStorage.hasStoredClient(){
client=try?clientStorage.loadClient()
}else{
client=try?clientStorage.createClient()
}
```
4. Next, create an identity. You will need to create a connection later.
```swift
/*
//Blocking call, do it on the background thread
*/
letmyIdentity=tryclient.makeIdentity()
```
5. Start the network follower.
```swift
letnetworkFollower=client.networkFollower
/*
//Blocking call, do it on the background thread
//Time is in millisecond
*/
trynetworkFollower.start(timeoutMS:30_000)
```
6. Finally, wait for the network to be connected.
```swift
/*
//Blocking call, do it on the background thread
//Time is in millisecond
*/
letisConnected=client.waitForNetwork(30_000)
guardisConnectedelse{/* 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.
1. First, to initiate a connection with the remote, use an unauthenticated connection. (To get remote identity, please refer to [this](https://git.xx.network/elixxir/client/-/blob/release/restlike/README.md) )
//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.
*/
letrequest=RestlikeMessage(
version:Int,
headers:YOUR_HEADERS,
content:YOUR_JSON_ENCODED_MODEL,
method:Int,
Uri:String,
error:String)
```
4. Send your request and wait for a response.
```swift
/*
//Blocking call, do it on the background thread
//Response is RestLikeMessage object
*/
letresponse=try?restLikeRequestSender.send(
client.getId(),
connection!.getId(),
request)
/*
//Jumping to the main thread to update any UI with the response
*/
DispatchQueue.main.async{
letmessage=response
//Use the response message here to update UI
}
```
## Send E2E message
1. Initiate a connection with the remote. We will use an unauthenticated connection. (To get remote identity, please refer to [this](https://git.xx.network/elixxir/client/-/blob/release/restlike/README.md) )
```swift
/*
//Blocking call, do it on the background thread
// You need your remote identity.
*/
letconnection=tryclient.connect(
withAuthentication:false,
recipientContact:REMOTE_IDENTITY,
myIdentity:myIdentity)
```
2. Setup your messages listener.
```swift
/*
//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){messagein
/*
//Jumping to the main thread to update any UI with the message
*/
DispatchQueue.main.async{
letmsg=message
//Use the received message here to update UI
}
}
```
3. Send the message and wait to get the send report, which contains the delivery status.
```swift
/*
//Blocking call, do it on the background thread
//messageType value must be same as the value you used to setup your messages listener
*/
letsendReport=tryconnection?.send(
messageType:Int,
payload:YOUR_JSON_ENCODED_MODEL)
```
4. Wait for the message to be delivered and update the UI if needed.