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

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

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

// Create a message draft.
// Assuming you have a reference of type "ChannelImpl" named "channel"
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"
messageDraft.addMention(offset: 33, length: 4, target: MentionTarget.url(url: "https://example.com"))

// 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

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

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

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

// Define the listener conforming to MessageDraftChangeListener protocol.
// You can also use our ClosureMessageDraftChangeListener class to reduce the need for your custom types to implement the MessageDraftChangeListener protocol
class LinkSuggestionListener: MessageDraftChangeListener {
func onChange(messageElements: [MessageElement], suggestedMentions: any FutureObject<[SuggestedMention]>) {
// Update UI with message elements
// This function is your own function for updating UI
updateUI(with: messageElements)
// Asynchronously process suggested URL mentions
suggestedMentions.async { result in
switch result {
case .success(let mentions):
// This is your own function for updating URL suggestions
updateLinkSuggestions(with: mentions)
case .failure(let error):
print("Error retrieving URL suggestions: \(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

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

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

// Assuming you have a reference of type "MessageImpl" named "message".
// 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
}
default:
return nil
show all 27 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

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

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

// Assuming you have a reference of type "ChatImpl" named "chat".
Task {
if let channel = try await chat.getChannel(channelId: "your-channel") {
if let message = try await channel.getMessage(timetoken: 16200000000000000) {
if let textLinks = message.textLinks, !textLinks.isEmpty {
for textLink in textLinks {
debugPrint("Link: \(textLink.link), Start Index: \(textLink.startIndex), End Index: \(textLink.endIndex)")
}
} else {
debugPrint("The message does not contain any text links.")
}
} else {
debugPrint("Message does not exist")
}
} else {
show all 18 lines
Last updated on