PrivMX DOCS
Swift

Inbox Entries

All the data sent by someone to an Inbox is called an Entry. In Threads and Stores, a user must be assigned to the Module to send data. In Inboxes, however, anyone who has Inbox ID can send a reply (assuming they have the Bridge URL and Solution ID).

Inboxes allow users to receive encrypted entries, either from external or internal sources.

  • Internal sources are people who have access to an Inbox, or are simply registered in a given Context. Those users are known to your application, and their entries can be linked to their accounts.

  • External sources are people from the outside; they do not have an account linked with your app. For example, someone who fills out an online contact form without the need to create an account.

About Entries

The structure of an entry and a brief description of its elements is outlined in the following table:

FieldTypeEncryptedDescription
databinaryContent of the entry
filesFile[]List of attached files
authorPubKeystringPublic key of the author

Define Data Structure

Inbox's architecture does not require you to use a specific data structure inside the entries. Before working with Inboxes, define the structure of your entries.

We recommend future-proofing your entries by choosing an easily modifiable format. It is a good idea to include both a type and a version field in the structure.

Example: Form with Text Answer

JSON
{
    “content”: {
        "answer": USER_PROVIDED_TEXT
    },
    “version”:number,
    “type”: "text_answer",
}

The type field allows future support for different types of entries like: "select_answer", or "multi_select". And in case of changing the schema, you can always distinguish between them using "version" field.

Remember that this is only an example and you should consider your app's requirements and limitations.

Before working with Inboxes, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.

Sample code on this page is based on the initial assumptions.

Entry's publicMeta and privateMeta fields support any kind of data formats encoded to byte arrays. Examples in this section use Swift serialization to JSON format.

Assumptions

In further examples, the following structures are used:

Swift
struct InboxPublicEntry:Codable{
    let name: String
    let surname: String
    let email: String
    let comment: String
}

Sending Entries

Having established public connection and inboxApi, you can send data to the Inbox:

Swift

let inboxID = "INBOX_ID"

let inboxPublicEntry = InboxPublicEntry(
    name: "name",
    surname: "surname",
    email: "email",
    comment: "comment")

guard let inboxPublicEntryData = try? JSONEncoder().encode(inboxPublicEntry) else {return}

guard let inboxHandle = try? publicEndpointSession?.inboxApi?.prepareEntry(in: inboxID, containing: inboxPublicEntryData, attaching: [], as: nil)  else {return}

try? publicEndpointSession?.inboxApi?.sendEntry(to: inboxHandle)

Listing Entries

Created entries can be listed by non-public connections created using PrivMXEndpoint.

Listing the most recent Entries in given Inbox

Swift
let inboxID = "INBOX_ID"
let startIndex:Int64 = 0
let pageSize:Int64 = 100

var entires = try? endpointSession?.inboxApi?.listEntries(
    from: inboxID,
    basedOn: privmx.endpoint.core.PagingQuery(skip: startIndex, limit: pageSize, sortOrder: .desc)
)

Reading Files from Entries

Files from Entries can be read by non-public connections created using PrivMXEndpoint class

Swift
let inboxID = "INBOX_ID"
let entryID = "ENTRY_ID"
let startIndex:Int64 = 0
let pageSize:Int64 = 100

let inboxEntry = try? endpointSession?.inboxApi?.readEntry(entryID)

var files =  inboxEntry?.files
var filesContents = files.map { file in
    try? endpointSession?.inboxApi?.openFile("\(file[0].info.fileId)")
}?.map{ fileHandle in
var content = Data()
var chunk : Data
repeat {
    chunk = (try? endpointSession?.inboxApi?.readFromFile(withHandle: fileHandle, length: PrivMXStoreFileHandler.RecommendedChunkSize)) ?? Data()
    content.append(chunk)
} while chunk.count == PrivMXStoreFileHandler.RecommendedChunkSize
return content
}

We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.

PrivMX Endpoint Swift v2.6

This package is not up to date with the core documentation. Some of the features you've seen described in other parts of the documentation might not be mentioned here. Those changes do not influence compatibility, however

On this page

Inbox Entries | PrivMX Docs