Skip to main content
This page contains iOS SDK code examples for User Experience extensions. For feature documentation, setup instructions, and extension settings, see User Experience Extensions.

How to Use Extensions with SDK

1

Enable in Dashboard

Login to CometChat Dashboard, select your app, then go to Chat & Messaging → Features and enable the extension.
2

Configure settings (if required)

Some extensions require API keys or additional configuration. Open the extension settings in the Dashboard.
3

Implement SDK methods

Use the code examples below to call extension APIs and extract extension data from messages.
4

Build your UI

Create UI components to display extension data (e.g., link previews, pinned messages, saved messages).

Bitly

Shorten long URLs in messages using Bitly.

Shorten URL

CometChat.callExtension(slug: "url-shortener-bitly", type: .post, endPoint: "v1/shorten", body: ["text": "Your message with URL https://yourdomain.com/very/very/long/url"], onSuccess: { (response) in
        // minifiedText from the extension
      }) { (error) in
        // Some error occured
      }
    }

Extract link preview metadata from messages.
if let metaData = textMessage.metaData, let injected = metaData["@injected"] as? [String : Any], let cometChatExtension = injected["extensions"] as? [String : Any], let linkPreviewDictionary = cometChatExtension["link-preview"] as? [String : Any], let linkArray = linkPreviewDictionary["links"] as? [[String: Any]] {
  guard let linkPreview = linkArray[safe: 0] else { return }
  if let linkTitle = linkPreview["title"] as? String { print(linkTitle) }
  // ... other fields
}

Message Shortcuts

Send predefined messages using shortcuts (e.g., !hb expands to “Happy birthday!”).

Fetch All Shortcuts

CometChat.callExtension(slug: "message-shortcuts", type: .get, endPoint: "v1/fetch", body: nil, onSuccess: { (response) in
  print("Shortcuts", response)
}) { (error) in
  print("Error", error?.errorCode, error?.errorDescription)
}

Modify Shortcuts

let body: [String: Any] = [
    "shortcuts": [
        "!hbd": "Happy birthday! Have fun!"
    ]
]

CometChat.callExtension(slug: "message-shortcuts", type: .post, endPoint: "v1/update", body: body, onSuccess: { (response) in
    print("Shortcuts updated successfully")
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Pin Message

Pin important messages in conversations.

Pin a Message

CometChat.callExtension(slug: "pin-message", type: .post, endPoint: "v1/pin", body: ["msgId": 280], onSuccess: { (response) in
    // { success: true }
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Unpin a Message

let body: [String: Any] = [
    "msgId": 111,
    "receiverType": "group",
    "receiver": "cometchat-guid-1"
]

CometChat.callExtension(slug: "pin-message", type: .delete, endPoint: "v1/unpin", body: body, onSuccess: { (response) in
    // { success: true }
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Fetch Pinned Messages

let receiverType = "group"
let receiver = "cometchat-guid-1"
let endPoint = "v1/fetch?receiverType=\(receiverType)&receiver=\(receiver)"

CometChat.callExtension(slug: "pin-message", type: .get, endPoint: endPoint, body: nil, onSuccess: { (response) in
    // { pinnedMessages: [] }
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Rich Media Preview

Extract rich media preview metadata from messages using Iframely.

Extract Rich Media Data

if let metaData = message.metaData, 
   let injected = metaData["@injected"] as? [String: Any], 
   let extensions = injected["extensions"] as? [String: Any], 
   let richMedia = extensions["rich-media"] as? [String: Any] {
    // Use richMedia data
    print("Rich Media:", richMedia)
}

Save Message

Save important messages for later access.

Save a Message

CometChat.callExtension(slug: "save-message", type: .post, endPoint: "v1/save", body: ["msgId": 111], onSuccess: { (response) in
    // { success: true }
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Unsave a Message

CometChat.callExtension(slug: "save-message", type: .delete, endPoint: "v1/unsave", body: ["msgId": 111], onSuccess: { (response) in
    // { success: true }
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Fetch Saved Messages

CometChat.callExtension(slug: "save-message", type: .get, endPoint: "v1/fetch", body: nil, onSuccess: { (response) in
    // { savedMessages: [] }
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Thumbnail Generation

Extract thumbnail URLs from image and video messages.

Extract Thumbnail Data

if let metaData = message.metaData, 
   let injected = metaData["@injected"] as? [String: Any], 
   let extensions = injected["extensions"] as? [String: Any], 
   let thumbnailGeneration = extensions["thumbnail-generation"] as? [String: Any],
   let attachments = thumbnailGeneration["attachments"] as? [[String: Any]] {
    for attachment in attachments {
        if attachment["error"] == nil,
           let data = attachment["data"] as? [String: Any],
           let thumbnails = data["thumbnails"] as? [String: Any] {
            let urlSmall = thumbnails["url_small"] as? String
            let urlMedium = thumbnails["url_medium"] as? String
            let urlLarge = thumbnails["url_large"] as? String
            // Use the URLs accordingly
        }
    }
}

TinyURL

Shorten long URLs in messages using TinyURL.

Shorten URL

CometChat.callExtension(slug: "url-shortener-tinyurl", type: .post, endPoint: "v1/shorten", body: ["text": "Your message with URL https://yourdomain.com/very/very/long/url"], onSuccess: { (response) in
    // minifiedText from the extension
}) { (error) in
    print("Error", error?.errorCode, error?.errorDescription)
}

Voice Transcription

Extract transcribed text from audio messages.

Extract Transcription Data

if let metaData = message.metaData, 
   let injected = metaData["@injected"] as? [String: Any], 
   let extensions = injected["extensions"] as? [String: Any], 
   let voiceTranscription = extensions["voice-transcription"] as? [String: Any],
   let transcribedMessage = voiceTranscription["transcribed_message"] as? String {
    print("Transcribed message:", transcribedMessage)
}