On this page

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.

Sample code

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.

1// Create a message draft.
2// Assuming you have a reference of type "ChannelImpl" named "channel"
3let messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered: channel.type != .public)
4// Update the text of the message draft
5messageDraft.update(text: "Hello Alex! I have sent you this link on the #offtopic channel.")
6// Add a URL mention to the word "link"
7messageDraft.addMention(offset: 33, length: 4, target: MentionTarget.url(url: "https://example.com"))
8
9// Additional logic can be implemented as needed
10// 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.

Sample code

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.

1// Assume the message reads: "Hello Alex! I have sent you this link on the #offtopic channel."
2// Remove the link mention
3messageDraft.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.

Sample code

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.

1// Define the listener conforming to MessageDraftChangeListener protocol.
2// You can also use our ClosureMessageDraftChangeListener class to reduce the need for your custom types to implement the MessageDraftChangeListener protocol
3class LinkSuggestionListener: MessageDraftChangeListener {
4 func onChange(messageElements: [MessageElement], suggestedMentions: any FutureObject<[SuggestedMention]>) {
5 // Update UI with message elements
6 // This function is your own function for updating UI
7 updateUI(with: messageElements)
8 // Asynchronously process suggested URL mentions
9 suggestedMentions.async { result in
10 switch result {
11 case .success(let mentions):
12 // This is your own function for updating URL suggestions
13 updateLinkSuggestions(with: mentions)
14 case .failure(let error):
15 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:

1message.getMessageElements()

Sample code

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.

1// Assuming you have a reference of type "MessageImpl" named "message".
2// Retrieve the message elements
3let messageElements = message.getMessageElements()
4
5// Filter the message elements to get only text links
6let textLinks = messageElements.compactMap {
7 switch $0 {
8 case let .link(text, target):
9 if case let .url(url) = target {
10 return URL(string: url)
11 } else {
12 return nil
13 }
14 default:
15 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:

1message.textLinks

Sample code

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.

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