Links

Swift Chat SDK lets you encode URLs that begin with www, http, or https (plain links) so that they can be rendered as links.

Generic referencing

Channel references, user mentions, and links are instances of MessageElement with different MentionTarget types.

addMention() lets you replace a plain link on the draft message to display a meaningful text in the published message.

Method signature

You can attach links to a draft message by calling the addMention() method with the target of MentionTarget.url.

Refer to the addMention() method for details.

Basic usage

Create the Hello Alex! I have sent you this link on the #offtopic channel. message where link is a URL.

// Create a message draft
if let messageDraft = channel?.createMessageDraft(isTypingIndicatorTriggered: channel?.type != .public) {

// Update the text of the message draft
messageDraft.update(text: "Hello Alex! I have sent you this link on the #offtopic channel.")

// Add a URL mention to the word "link"
let urlMentionTarget = MentionTarget.url(url: "https://example.com")
messageDraft.addMention(offset: 33, length: 4, target: urlMentionTarget)

// Additional logic can be implemented as needed
// For example, sending the draft or adding listeners
}

removeMention() lets you remove a previously added link from a draft message.

Method signature

You can remove links from a draft message by calling the removeMention() method at the exact offset where the link starts.

Refer to the removeMention() method for details.

Offset value

If you don't provide the position of the first character of the message element to remove, it isn't removed.

Basic usage

Remove the link from the Hello Alex! I have sent you this link on the #offtopic channel. message where link is a URL.

// assume the message reads
// Hello Alex! I have sent you this link on the #offtopic channel.`

// remove the link mention
messageDraft?.removeMention(offset: 33)

The message elements listener returns all links referenced in the draft message that match the provided 3-letter string from your app's keyset.

icon

Single listener

Method signature

You must add a message elements listener to receive link suggestions.

Refer to the addChangeListener() method for details.

Basic usage

// Create a message draft
if let messageDraft = channel?.createMessageDraft(isTypingIndicatorTriggered: channel?.type != .public) {

// Define the listener conforming to MessageDraftChangeListener protocol
class LinkSuggestionListener: MessageDraftChangeListener {
func onChange(messageElements: [MessageElement], suggestedMentions: any FutureObject<[SuggestedMention]>) {
// Update UI with message elements
updateUI(with: messageElements) // updateUI is your own function for updating UI

// Asynchronously process suggested URL mentions
suggestedMentions.async { result in
switch result {
case .success(let mentions):
updateLinkSuggestions(with: mentions) // updateLinkSuggestions is your own function for updating URL suggestions
case .failure(let error):
show all 27 lines

To return all text links in a given message, use the getMessageElements() method.

Method signature

This method has the following signature:

message.getMessageElements()

Basic usage

Get all text links included in the message with the 16200000000000000 timetoken.

// Assuming you have a message object
let message = // your message object

// Retrieve the message elements
let messageElements = message.getMessageElements()

// Filter the message elements to get only text links
let textLinks = messageElements.compactMap {
switch $0 {
case let .link(text, target):
if case let .url(url) = target {
return URL(string: url)
} else {
return nil
}
show all 29 lines

You can access the textLinks property of the Message object to return all text links in a given message.

Method signature

This is how you can access the property:

message.textLinks

Basic usage

Get all text links included in the message with the 16200000000000000 timetoken.

// Reference the "your-channel" channel
chat?.getChannel(channelId: "your-channel") { result in
switch result {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
// Fetch the message using the timetoken
channel.getMessage(timetoken: 16200000000000000) { messageResult in
switch messageResult {
case let .success(message):
if let message = message {
// Access the textLinks property
if let textLinks = message.textLinks, !textLinks.isEmpty {
debugPrint("The message contains the following text links:")
for textLink in textLinks {
show all 34 lines
Last updated on