> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Pinned Messages

> Every message pinned in one conversation, drawn as its bubble, with jump-to-message, unpin and a long-press menu.

`CometChatPinnedMessages` lists the messages pinned in one conversation, newest pin first.
Each row is the message's own bubble under "name • date". Tap a row to jump to the message;
long-press it for Message Information, Copy, Translate, Unpin and Delete; swipe it to unpin.

> [!WARNING]
>
> **Pin Message must be enabled for your app** in the CometChat Dashboard. Until it is, the
> message list offers no Pin option and this screen has nothing to show. Read the switch
> with `CometChatSession.isPinMessageEnabled()` to gate your own entry point.

![CometChatPinnedMessages showing the pinned messages of a conversation](https://mintcdn.com/cometchat-22654f5b/pWDsskjSUHgot30F/images/ui-kit/ios/v6/docs-overview-pinned-messages.png?fit=max\&auto=format\&n=pWDsskjSUHgot30F\&q=85\&s=04016fe6d1d764d131331329775c3cad)

***

## Where It Fits

A pin is conversation-wide, so the screen takes the chat's `User` or `Group` and shows the
same set to everyone in it. Messages are pinned and unpinned from the
[message list](https://www.cometchat.com/docs/ui-kit/ios/v6/message-list.md)'s menu; this screen is the read view.

> [tab] SwiftUI
>
> ```
> CometChatMessageHeader ── ⋯ ── "Pinned messages"
>                                    │ onPinnedMessagesClick
>                                    ▼
>                     ┌───────────────────────────────┐
>                     │ CometChatPinnedMessages ← here │
>                     └───────────────────────────────┘
>                                    │ onMessageClick(message)
>                                    ▼
>                CometChatMessageList(goToMessageId:) — jumps to it
> ```

> [tab] UIKit
>
> Open it from the [message header](https://www.cometchat.com/docs/ui-kit/ios/v6/message-header.md)'s Pinned Messages entry
> (`showPinnedMessagesOption` and `set(onPinnedMessagesClick:)`). Jump the list to a message
> with `CometChatMessageList.goToMessage(withId:)`.

***

## Quick Start

Open it from the header's Pinned Messages entry, and jump the list to the message a row
hands you:

> [tab] SwiftUI
>
> ```swift theme={null}
> struct GroupChatScreen: View {
>     let group: CometChatSDK.Group
>     @State private var showPinned = false
>     @State private var jumpTo: MessageID?
>
>     var body: some View {
>         // … `header` and the composer as bars over the list …
>         CometChatMessageList(group: group, goToMessageId: $jumpTo)
>             .sheet(isPresented: $showPinned) {
>                 NavigationStack {
>                     CometChatPinnedMessages(group: group,
>                                             onMessageClick: { message in
>                                                 jumpTo = message.id
>                                                 showPinned = false
>                                             },
>                                             onBack: { showPinned = false })
>                 }
>             }
>     }
>
>     var header: some View {
>         CometChatMessageHeader(group: group,
>                                showPinnedMessagesOption: true,
>                                onPinnedMessagesClick: { showPinned = true })
>     }
> }
> ```
>
> Host the header and the composer over the list as [Conversation List + Message View](https://www.cometchat.com/docs/ui-kit/ios/v6/conversation-message-view.md#step-2-set-up-the-message-screen) shows.
>
> Push it inside a `NavigationStack`: it uses the system navigation bar with an inline title.
> The back chevron calls `onBack` and nothing else. With `hideBackIcon: true` the system's
> own back button shows instead — the choice for a pushed screen.

> [tab] UIKit
>
> ```swift theme={null}
> final class GroupChatViewController: UIViewController {
>     let group: CometChatSDK.Group
>     let header = CometChatMessageHeader()
>     let list = CometChatMessageList()
>
>     init(group: CometChatSDK.Group) {
>         self.group = group
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         header.set(group: group)
>         header.showPinnedMessagesOption = true
>         header.set(onPinnedMessagesClick: { [weak self] in self?.showPinned() })
>         list.set(group: group)
>         // … lay out the header, the list and a composer
>     }
>
>     private func showPinned() {
>         let pinned = CometChatPinnedMessages(group: group)
>             .set(onMessageClicked: { [weak self] message in
>                 self?.navigationController?.popViewController(animated: true)
>                 self?.list.goToMessage(withId: message.id)
>             })
>         navigationController?.pushViewController(pinned, animated: true)
>     }
> }
> ```
>
> `CometChatPinnedMessages` is a view controller — push it. The back chevron pops the screen.
> `set(onBack:)` replaces that; `hideBackButton = true` removes it.

***

## Rows

> [tab] SwiftUI
>
> Each row is the message's bubble — the same bubble the message list draws, with your
> `bubbleFactories` — on the leading side with the sender's avatar, under "name • date".
> Your own messages read "You" and keep the outgoing colours. Rows show no receipt and no
> "N replies", and the bubble's content does not take taps: a tap anywhere on the row is
> `onMessageClick`.
>
> The date is `dateTimeFormatter`'s `otherDays` answer when you pass one; its `time` answer
> replaces the bubble's time.

> [tab] UIKit
>
> Each row is the message list's bubble cell, `CometChatMessageBubble`, on the left with the
> sender's avatar and colours, under "name • date". Your own messages read "You" and keep the
> outgoing colours. Rows show no receipt and no "N replies", and the bubble's content does
> not take taps: a tap anywhere on the row is `onMessageClicked`. The rows draw the Kit's
> built-in bubbles — the screen takes no templates.

***

## Actions and Events

### Callback Methods

> [tab] SwiftUI
>
> | Prop | Signature | When |
> | - | - | - |
> | `onMessageClick` | `(BaseMessage) -> Void` | a row is tapped |
> | `onBack` | `() -> Void` | the back chevron is tapped; with no callback it does nothing |
> | `onError` | `(CometChatError) -> Void` | a load, a refresh, an unpin, a delete or a translation fails |

> [tab] UIKit
>
> | Setter | Signature | When |
> | - | - | - |
> | `set(onMessageClicked:)` | `(BaseMessage) -> Void` | a row is tapped |
> | `set(onBack:)` | `() -> Void` | the back chevron is tapped; with no callback it pops |
> | `set(onError:)` | `(CometChatException) -> Void` | a load, a refresh, an unpin, a delete or a translation fails |

### Gestures

| Gesture | Does |
| - | - |
| tap | calls the message-click callback with the SDK's `BaseMessage` |
| long press | a menu of **Info**, **Copy**, **Translate**, **Unpin**, **Delete** — each when the message list's rules allow it for that message and your role |
| trailing swipe | **Unpin message**, when Unpin is allowed |
| pull down | refresh, keeping the rows up until the answer lands |

Unpin and Delete ask first. Confirming removes the row at once; if the server refuses, the
row comes back where it was, a toast says why, and `onError` is called. **Info** opens
[Message Information](https://www.cometchat.com/docs/ui-kit/ios/v6/message-information.md) in a sheet. There is no
Pin option on this screen.

### SDK Events (Real-Time, Automatic)

The screen keeps itself current from its own SDK listener and the Kit's event bus: a pin or
unpin made anywhere in the app adds or removes the row; an edit redraws it; a message deleted
elsewhere leaves the list. After a reconnect the list is fetched again.

It loads every page up front — 30 at a time, until there are at least 100 rows — in the
server's order, newest pin first. A failure on any page shows the error state.

***

## Functionality

> [tab] SwiftUI
>
> | Prop | Type | Default |
> | - | - | - |
> | `user` | `CometChatSDK.User?` | `nil` |
> | `group` | `CometChatSDK.Group?` | `nil` |
> | `store` | `PinnedMessagesStore?` | `nil` |
> | `textFormatters` | `[any CometChatTextFormatter]?` | `[CometChatMentionsFormatter()]` |
> | `style` | `CometChatPinnedMessagesStyle` | |
> | `bubbleFactories` | `[any BubbleFactory]` | `[]` |
> | `dateTimeFormatter` | `(any CometChatDateTimeFormatter)?` | `nil` |
> | `title` | `String?` | "Pinned Messages" |
> | `hideToolbar` | `Bool` | `false` |
> | `hideBackIcon` | `Bool` | `false` |
>
> Pass a user or a group.

> [tab] UIKit
>
> | Method or property | Type | Does |
> | - | - | - |
> | `init(user:group:pinnedMessagesStore:)` | | the conversation, and optionally your own store |
> | `set(user:)`, `set(group:)` | `CometChatSDK.User`, `CometChatSDK.Group` | the conversation |
> | `set(textFormatters:)` | `[any CometChatTextFormatter]` | text formatters for the bubbles |
> | `set(style:)`, `style` | `PinnedMessagesStyle` | the screen's style |
> | `hideBackButton` | `Bool` | hides the back chevron; `false` by default |
>
> There is no `title`, `hideToolbar` or `dateTimeFormatter` — set `title` on the controller.

***

## States

| State | Shows |
| - | - |
| loading | the bubble shimmer |
| empty | the pin art, "No pinned messages yet" and "Pin important messages to keep them easy to find." |
| error | "Oops!", "Something went wrong. Please try again." and **Retry** |

***

## Custom View Slots

> [tab] SwiftUI
>
> | Slot | Replaces |
> | - | - |
> | `loadingView` | the loading state |
> | `emptyView` | the empty state |
> | `errorView` | the error state — it receives the retry action |
>
> ```swift theme={null}
> CometChatPinnedMessages(
>     group: group,
>     loadingView: { AnyView(MySpinner()) },
>     emptyView: { AnyView(MyEmptyState()) },
>     errorView: { retry in AnyView(MyErrorState(onRetry: retry)) },
>     onError: { error in report(error) }
> )
> ```

> [tab] UIKit
>
> Replace a state's view with `set(loadingView:)`, `set(emptyView:)` or `set(errorView:)`.
> `set(onError:)` hears every failure — a load, a refresh, an unpin, a delete, a translation:
>
> ```swift theme={null}
> let pinned = CometChatPinnedMessages(group: group)
>     .set(emptyView: MyEmptyState())
>     .set(onError: { error in print(error.errorDescription) })
> ```

***

## Advanced Methods

### Store Access

> [tab] SwiftUI
>
> `PinnedMessagesStore` fetches, unpins, deletes, copies and translates, and holds `uiState`,
> `messages` and `count`. Pass your own to read them, or a seeded one for a preview — a seeded
> store never fetches:
>
> ```swift theme={null}
> #Preview {
>     NavigationStack {
>         CometChatPinnedMessages(store: PinnedMessagesStore(seeded: .content, messages: ChatMessage.samplePinned))
>     }
> }
> ```

> [tab] UIKit
>
> `pinnedMessagesStore` is the screen's `PinnedMessagesStore` — `uiState`, `messages`,
> `count`, and `refresh()` / `reload()`. Pass your own to `init(user:group:pinnedMessagesStore:)`
> to share or seed it.

### Subclassing

> [tab] SwiftUI
>
> `CometChatPinnedMessages` is a `View` struct. Customise it with its slots, style and bubble factories.

> [tab] UIKit
>
> The view controller is `open`. Override `defaultSetup()`, `setupStyle()`, the
> state views (`showLoadingView()`, `showEmptyView()`, `showErrorView()`, `hideStateViews()`),
> `reloadData()`, `presentInformation(for:)` and the table methods:
>
> ```swift theme={null}
> final class TitledPinnedMessages: CometChatPinnedMessages {
>     override func defaultSetup() {
>         super.defaultSetup()
>         title = "Pinned"
>     }
> }
> ```

***

## Style

> [tab] SwiftUI
>
> `CometChatPinnedMessagesStyle` is sparse: set only what you change. The bubbles take
> `messageListStyle` — the message list's own style — so a pinned bubble matches the chat.
>
> | Property | Default |
> | - | - |
> | `backgroundColor` | `background1` |
> | `titleColor`, `titleFont` | `textPrimary`, `heading2Bold` |
> | `senderNameColor`, `senderNameFont` | `primary`, `caption1Medium` — the header's name |
> | `dateColor`, `dateFont` | `textSecondary`, `caption1Regular` — the header's bullet and date |
> | `emptyStateTitle…`, `emptyStateSubtitle…`, `emptyStateIcon` | `heading3Bold` / `bodyRegular`, the pin art |
> | `errorStateTitle…`, `errorStateSubtitle…`, `errorStateIcon` | `heading3Bold` / `bodyRegular`, the error art |
> | `retryButtonTextColor`, `retryButtonTextFont`, `retryButtonBackgroundColor`, `retryButtonCornerRadius` | `primaryButtonText`, `buttonMedium`, `primaryButtonBackground`, 8 |
> | `loadingStateSurfaceColor` | `background3` — the shimmer's blocks |
> | `unpinActionBackgroundColor` | the swipe action — `error` |
> | `messageListStyle` | the bubbles |
>
> ```swift theme={null}
> var style = CometChatPinnedMessagesStyle()
> style.senderNameColor = .indigo
> style.unpinActionBackgroundColor = .orange
>
> CometChatPinnedMessages(group: group, style: style)
> ```

> [tab] UIKit
>
> Set the static default for every screen, or one screen's `style`. The bubbles take the
> message list's static styles.
>
> ```swift theme={null}
> CometChatPinnedMessages.style.unpinActionBackgroundColor = .systemOrange
>
> let pinned = CometChatPinnedMessages(group: group)
> pinned.style.bubbleHeaderTextColor = .systemIndigo
> ```
>
> | Property | Default |
> | - | - |
> | `backgroundColor` | `background1` |
> | `titleColor`, `titleFont` | `textPrimary`, 17 bold |
> | `navigationBarTintColor`, `navigationBarItemsTintColor` | the bar; the chevron in `iconTintHighlight` |
> | `bubbleHeaderTextColor`, `bubbleHeaderFont` | `primary`, `caption1Medium` — the header's name |
> | `bubbleDateTextColor`, `bubbleDateFont` | `textSecondary`, `caption1Regular` — the header's date |
> | `emptyTitleTextColor`, `emptyTitleTextFont`, `emptySubTitleTextColor`, `emptySubTitleFont` | `heading3Bold` / `bodyRegular` |
> | `errorTitleTextColor`, `errorTitleTextFont`, `errorSubTitleTextColor`, `errorSubTitleFont` | `heading3Bold` / `bodyRegular` |
> | `retryButtonTextColor`, `retryButtonTextFont`, `retryButtonBackgroundColor`, `retryButtonCornerRadius` | Retry; radius 8 |
> | `unpinIconTint`, `unpinActionBackgroundColor` | white, `error` — the swipe |

***

## Next Steps

> [card] [Message List](https://www.cometchat.com/docs/ui-kit/ios/v6/message-list.md)
>
> Pin and unpin messages, and jump to one

> [card] [Message Header](https://www.cometchat.com/docs/ui-kit/ios/v6/message-header.md)
>
> Open the screen from the header's entry

> [card] [Message Information](https://www.cometchat.com/docs/ui-kit/ios/v6/message-information.md)
>
> Delivery and read details for a message

> [card] [Saved Messages](https://www.cometchat.com/docs/ui-kit/ios/v6/saved-messages.md)
>
> The messages a user saved for themselves


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.