Webex Messaging
API Guide
This article provides detailed information on the messaging features and methods provided by the Webex iOS Messaging SDK.
The SDK lets you easily implement real-time messaging capabilities including posting new messages, and deleting, listing, and editing existing messages.
anchorBefore Continuing: Sync Webex Spaces
anchorBefore working with spaces or messages within them, ensure that all spaces are synced using the following method:
// Indicates if the latest conversations have been synced to the local data warehouse.
// Returns true if the sync is completed.
// Returns false if the sync is in progress.
webex.spaces.isSpacesSyncCompleted
// The callback handler for sync status changes.
webex.spaces.onSyncingSpacesStatusChanged = { isSpacesSyncInProgress in
// isSpacesSyncInProgress is true if a space sync is in progress.
// isSpacesSyncInProgress is false if a space sync is complete.
}

anchorPost a Text Message to a Person by ID
anchorTo post a text message to a particular participant using their personId:
webex.messages.post(Message.Text.plain(plain: "text..."), toPerson: "{personId}", withFiles: nil){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
Post a Text Message to a Person by Email
To post a text message to a person using their email address:
guard let emailAddress = EmailAddress.fromString("{person email address}") else{ return }
webex.messages.post(Message.Text.plain(plain: "text..."), toPersonEmail: emailAddress, withFiles: nil){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
Post a Text Message to a Room
To post a text message to a room using its spaceId:
webex.messages.post(Message.Text.plain(plain: "text..."), toSpace: "{spaceId}", withFiles: nil){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
Post Single Mentions
To post a message that includes mention(s) of individual person(s):
//create person mention items with personId and the mention's start and end positions in the message text
let mentionItem1 = Mention.person(MentionPos(id: "{personId1}", start: start1, end: end1))
let mentionItem2 = Mention.person(MentionPos(id: "{personId2}", start: start2, end: end2))
let mentionArray = [mentionItem1, mentionItem2]
webex.messages.post(Message.Text.plain(plain: "text..."), toSpace: "{spaceId}", mentions: mentionArray, withFiles: nil){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
Post a Group Mention
To post a message that includes a group mention:
//create a group mention item with the mention's start and end positions in the message text.
let mentionItem = Mention.all(MentionPos(id: "", start: start, end: end))
let mentionArray = [mentionItem]
webex.messages.post(Message.Text.plain(plain: "text..."), toSpace: "{spaceId}", mentions: mentionArray, withFiles: nil){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
anchorPost Local Files
anchorTo post a message with attached files using local paths:
guard let fileItem1 = LocalFile.init(path: "{local file path}"),
let fileItem2 = LocalFile.init(path: "{local file path}", name: "{file name}", mime: "{file mime type}") else {return}
let fileArray = [fileItem1, fileItem2]
webex.messages.post(Message.Text.plain(plain: "{text...}"), toSpace: "{space id}", withFiles: fileArray){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
Post Files with Uploading Progress
To post a message with a file and monitor the uploading progress:
guard let fileItem = LocalFile.init(path: "{local file path}", name: "{file name}", mime: "{file mime type}",
progressHandler: { (progress) in
// progress as double
}) else {return}
webex.messages.post(Message.Text.plain(plain: "{text...}"), toSpace: "{space id}", withFiles: [fileItem]){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let error):
// ...
break
}
}
Post Files with a Thumbnail Preview
To post a message with a file that includes a thumbnail:
let fileThumb = LocalFile.Thumbnail.init(path: "{file path}", mime: "{mime type}", width: 320, height: 400)
guard let fileItem = LocalFile.init(path: "{local file path}", name: "{file name}", mime: "{file mime type}",
thumbnail: fileThumb, progressHandler: { (progress) in
// progress as double
}) else {return}
webex.messages.post(Message.Text.plain(plain: "{text...}"), toSpace: "{space id}", withFiles: [fileItem]){ result in
switch result {
case .success(let message):
// ...
break
case .failure(let err):
// ...
break
}
}
anchorDelete a Message
anchorTo delete a message using its messageId:
webex.messages.delete(messageId: "{message id}", completionHandler: { (result) in
switch result{
case .success(_):
// delete operation successful.
break
case .failure(let err):
// delete operation failed
break
}
})
anchorList Messages Before a Specific Date
anchorTo list messages before a particular date point within a room, with max specifying the maximum number of messages to return:
let before = Before.date(date)
webex.messages.list(spaceId: "{space id}", max: 50, before: before) { result in
switch result {
case .success(let messages):
// ...
break
case .failure(let err):
// ...
break
}
}
anchorList Messages Before a Specific Message
anchorTo list messages before a particular message within a room:
let beforeMessage = Before.message("{message id}")
webex.messages.list(spaceId: "{space id}", max: 50, before: beforeMessage) { result in
switch result {
case .success(let messages):
// ...
break
case .failure(let err):
// ...
break
}
}
anchorList Messages That Mentioned Your ID
anchorTo list messages in which your ID is mentioned within a group room, get your person ID and pass it in the mentionedPeople array:
webex.people.getMe { result in
guard case .success(let me) = result, let myId = me.id else { return }
let mentioned = [Mention.person(MentionPos(id: myId, start: 0, end: 0))]
webex.messages.list(spaceId: "{space id}", max: 50, mentionedPeople: mentioned) { result in
switch result {
case .success(let messages):
// ...
break
case .failure(let err):
// ...
break
}
}
}
anchorMessage Reception Events
anchorTo handle message reception events:
webex.messages.onEvent = { messageEvent in
switch messageEvent{
case .messageReceived(let message):
// received message...
break
case .messageDeleted(let messageId):
// deleted messageId...
break
default:
break
}
}
anchorDownload Simple Files
anchorWhen a message with files is received, this code will download each file to default location:
webex.messages.onEvent = { messageEvent in
switch messageEvent{
case .messageReceived(let message):
if let files = message.files {
for file in files {
webex.messages.downloadFile(file) { result in
if let localUrl = result.data{
// file downloaded successfully in localUrl ...
}
}
}
}
break
case .messageDeleted(let messageId):
// ...
break
default:
break
}
}
anchorDownload Files to a Particular Destination
anchorThis code snippet is similar to the previous one, but it specifies a destination directory where the files should be downloaded:
webex.messages.onEvent = { messageEvent in
switch messageEvent{
case .messageReceived(let message):
let destination = URL.init(string: "{destination directory}")
if let files = message.files {
for file in files {
webex.messages.downloadFile(file, to: destination) { result in
if let localUrl = result.data{
// file downloaded successfully in localUrl ...
}
}
}
}
break
case .messageDeleted(let messageId):
// ...
break
default:
break
}
}
anchorDownload Files with a Progress Handler
anchorIn addition to downloading the files, this snippet provides a progress handler that reports the progress of the file download:
webex.messages.onEvent = { messageEvent in
switch messageEvent{
case .messageReceived(let message):
if let files = message.files {
for file in files {
webex.messages.downloadFile(file, progressHandler: { progress in
// progress in double
}) { result in
if let localUrl = result.data{
// file downloaded successfully in localUrl ...
}
}
}
}
break
case .messageDeleted(let messageId):
// ...
break
default:
break
}
}
anchorDownload Thumbnails
anchorTo download the thumbnails of the files included in a message:
webex.messages.onEvent = { messageEvent in
switch messageEvent{
case .messageReceived(let message):
if let files = message.files {
for file in files {
webex.messages.downloadThumbnail(for: file) { result in
if let localUrl = result.data{
// file downloaded successfully in localUrl ...
}
}
}
}
break
case .messageDeleted(let messageId):
// ...
break
default:
break
}
}
anchorDownload Thumbnails to a Particular Destination
anchorSimilar to downloading files, this snippet specifies a destination for the downloaded thumbnails:
webex.messages.onEvent = { messageEvent in
switch messageEvent{
case .messageReceived(let message):
let destination = URL.init(string: "{destination directory}")
if let files = message.files {
for file in files {
webex.messages.downloadThumbnail(for: file, to: destination) { result in
if let localUrl = result.data{
// file downloaded successfully in localUrl ...
}
}
}
}
break
case .messageDeleted(let messageId):
// ...
break
default:
break
}
}
anchorSend a Read Receipt
anchorTo send a read receipt for a message:
webex.messages.markAsRead(spaceId: spaceId, messageId: messageId, completionHandler: { result in
switch result {
case .success(_):
// Read receipt sent successfully.
case .failure(let error):
// Failed to send read receipt.
}
})
anchorMembership Events
anchorTo handle membership change events:
webex.memberships.onEvent = { membershipEvent in
switch membershipEvent {
case .created(let membership):
// A new membership was created.
case .deleted(let membership):
// A membership was deleted.
case .update(let membership):
// A membership was updated.
case .messageSeen(let membership, let lastSeenId):
// A message was seen by a member.
}
}
anchorList Memberships with Read Status
anchorTo get the read statuses of all memberships in a space:
webex.memberships.listWithReadStatus(spaceId: spaceId, completionHandler: { response in
switch response.result {
case .success(let readStatuses):
// Process read statuses.
case .failure(let error):
// Handle error.
}
})
anchorSpace Events
anchorTo handle space change events:
webex.spaces.onEvent = { spaceEvent in
switch spaceEvent {
case .create(let space):
// A new space was created.
case .update(let space):
// A space was updated.
case .spaceCallStarted(let spaceId):
// A call started in the space.
case .spaceCallEnded(let spaceId):
// A call ended in the space.
}
}
anchorGet Space Read Status
anchorTo get the read status of a space for the logged-in user:
webex.spaces.getWithReadStatus(spaceId: spaceId, completionHandler: { response in
switch response.result {
case .success(let spaceInfo):
if let lastActivityDate = spaceInfo?.lastActivityDate,
let lastSeenDate = spaceInfo?.lastSeenActivityDate,
lastActivityDate > lastSeenDate {
// Space has unread messages.
} else {
// Space is fully read.
}
case .failure(let error):
// Handle error.
}
})