> ## 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.

# Pin & Save Messages

> Add pinned messages, saved messages and pinned conversations to a CometChat iOS UI Kit v6 app with the built-in menu options and screens.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "components": ["CometChatMessageList", "CometChatMessageHeader", "CometChatPinnedMessages", "CometChatSavedMessages", "CometChatConversations"],
>   "feature switches": "CometChatSession.isPinMessageEnabled(), CometChatSession.isSaveMessageEnabled() — set server-side",
>   "pinned entry": "CometChatMessageHeader showPinnedMessagesOption + onPinnedMessagesClick (SwiftUI) / showPinnedMessagesOption + set(onPinnedMessagesClick:) (UIKit)",
>   "jump to a message": "CometChatMessageList goToMessageId: Binding (SwiftUI) / goToMessage(withId:) (UIKit)",
>   "events": "messagePinned, messageUnpinned, messageSaved, messageUnsaved"
> }
> ```

**Pin** a message to keep it at hand for everyone in the conversation; **save** a message to
keep a private bookmark across all your conversations; **pin a conversation** to keep it at
the top of the list. The message list's menu does the pinning and saving — this guide adds the
screens that show the results.

## Overview

| Feature | Who sees it | Where it appears |
| - | - | - |
| Pin message | everyone in the conversation | the menu's **Pin** / **Unpin**, a pin mark on the bubble, `CometChatPinnedMessages` |
| Save message | only you | the menu's **Save** / **Unsave**, `CometChatSavedMessages` |
| Pin conversation | only you | Pin / Unpin on a conversation's swipe actions and long-press menu |

## Prerequisites

* The Kit is set up and a user is logged in — see [Getting Started](https://www.cometchat.com/docs/ui-kit/ios/v6/getting-started-swiftui.md).
* Pinned and saved messages are switched on for your app in the CometChat dashboard. The Kit
  reads the switches through `CometChatSession.isPinMessageEnabled()` and
  `CometChatSession.isSaveMessageEnabled()`; with a switch off, its options and screens do
  not appear.

## Pin & Save in the Message List

Nothing to wire: `CometChatMessageList` adds **Pin** / **Unpin** and **Save** / **Unsave** to
a message's long-press menu when the feature is on. Pinning puts a pin mark on the bubble;
both announce themselves on the event bus, so every other surface updates.

## Step 1: Open Pinned Messages from the Chat Header

Turn on the header's pinned-messages entry and open `CometChatPinnedMessages` for the same
chat. When the user taps a pinned message, close the screen and jump the list to it.

> [tab] SwiftUI
>
> ```swift ChatView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct PinnedChatView: View {
>     let user: CometChatSDK.User?
>     let group: CometChatSDK.Group?
>     @State private var showsPinned = false
>     @State private var jumpTo: MessageID?
>
>     var body: some View {
>         // … `header` and the composer as bars over the list …
>         // The list jumps to the message, highlights it, and resets the binding to nil.
>         CometChatMessageList(user: user, group: group, goToMessageId: $jumpTo)
>             .sheet(isPresented: $showsPinned) {
>                 NavigationStack {
>                     CometChatPinnedMessages(
>                         user: group == nil ? user : nil,
>                         group: group,
>                         onMessageClick: { message in
>                             showsPinned = false
>                             jumpTo = message.id
>                         },
>                         onBack: { showsPinned = false }
>                     )
>                 }
>             }
>     }
>
>     var header: some View {
>         CometChatMessageHeader(
>             user: user,
>             group: group,
>             showPinnedMessagesOption: true,
>             onPinnedMessagesClick: { showsPinned = true }
>         )
>     }
> }
> ```

> [tab] UIKit
>
> ```swift ChatViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class PinnedChatViewController: UIViewController {
>     let user: CometChatSDK.User
>     let header = CometChatMessageHeader()
>     let messageList = CometChatMessageList()
>
>     init(user: CometChatSDK.User) {
>         self.user = user
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         header.set(user: user)
>         messageList.set(user: user)
>
>         header.showPinnedMessagesOption = true
>         header.set(onPinnedMessagesClick: { [weak self] in self?.openPinnedMessages() })
>         // … lay out the header, the list and a composer …
>     }
>
>     private func openPinnedMessages() {
>         let pinned = CometChatPinnedMessages(user: user)
>         pinned.set(onMessageClicked: { [weak self] message in
>             guard let self else { return }
>             self.navigationController?.popToViewController(self, animated: true)
>             self.messageList.goToMessage(withId: message.id)
>         })
>         navigationController?.pushViewController(pinned, animated: true)
>     }
> }
> ```

For a group chat pass the group — `CometChatPinnedMessages(group:)` — not the user.

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.

## Step 2: Open Saved Messages from Your App Chrome

Saved messages belong to the user, not to one chat, so open the screen from your own chrome —
a profile menu, a settings row. A tap hands you the message; open its conversation there.

> [tab] SwiftUI
>
> ```swift theme={null}
> struct SavedView: View {
>     @State private var opened: BaseMessage?
>
>     var body: some View {
>         CometChatSavedMessages(onMessageClick: { message in opened = message })
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let saved = CometChatSavedMessages()
>     .set(onMessageClicked: { message in
>         print("open the chat of", message.id)
>     })
> navigationController?.pushViewController(saved, animated: true)
> ```

A saved message's conversation is its group when `receiverType` is `.group`; otherwise it is
the other person — the receiver when you sent it, the sender when you did not. Open that chat
and jump to the message as in Step 1.

## Pin Conversations

`CometChatConversations` shows **Pin** / **Unpin** in a row's swipe actions and long-press menu
when the feature is on for your app. Pinning happens at once; unpinning asks first. A pinned
row stays above the rest. A conversation an administrator pinned shows no unpin — the server
refuses it.

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatConversations(hidePinOption: true)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let conversations = CometChatConversations()
> conversations.hidePinConversationOption = true
> ```
>
> `enablePinConversation` ships **on**.

## Limits and Errors

The caps — pins per conversation, saves per user, pinned conversations per user — are app
settings. When a user hits one, the Kit shows a toast that names the limit; you handle nothing.
If you build your own pin or save control on the SDK, handle these codes:

| Code | Meaning |
| - | - |
| `ERR_PERMISSION_DENIED` | The user's role may not pin or unpin here. |
| `ERR_PINNED_MESSAGES_LIMIT_EXCEEDED` | The conversation's pin cap was reached. |
| `ERR_SAVED_MESSAGES_LIMIT_EXCEEDED` | The user's save cap was reached. |

## Live Updates

Pinning, unpinning, saving and unsaving through the Kit are announced on the event bus, so the
bubble's pin mark, Pinned Messages and Saved Messages stay in step without a refetch. For your
own UI, listen to them:

> [tab] SwiftUI
>
> ```swift theme={null}
> .onCometChatMessagePinned { message in print("pinned", message.id) }
> .onCometChatMessageSaved { message in print("saved", message.id) }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> final class PinWatcher: NSObject, CometChatMessageEventListener {
>     func start() { CometChatMessageEvents.addListener("pin-watcher", self) }
>
>     func onMessagePinned(message: BaseMessage) { print("pinned", message.id) }
>     func onMessageSaved(message: BaseMessage) { print("saved", message.id) }
> }
> ```

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Pin / save options | built into `CometChatMessageList` | built into `CometChatMessageList` |
| Pinned entry in the header | `showPinnedMessagesOption: true`, `onPinnedMessagesClick:` | `showPinnedMessagesOption = true`, `set(onPinnedMessagesClick:)` |
| Pinned messages screen | `CometChatPinnedMessages(user:group:onMessageClick:)` | `CometChatPinnedMessages(user:group:)`, `set(onMessageClicked:)` |
| Jump to a message | `goToMessageId:` | `goToMessage(withId:)` |
| Saved messages screen | `CometChatSavedMessages(onMessageClick:)` | `CometChatSavedMessages()`, `set(onMessageClicked:)` |
| Hide conversation pin | `hidePinOption: true` | `hidePinConversationOption = true` |

## Next Steps

> [card] [Pinned Messages](https://www.cometchat.com/docs/ui-kit/ios/v6/pinned-messages.md)
>
> The pinned-messages screen in full

> [card] [Saved Messages](https://www.cometchat.com/docs/ui-kit/ios/v6/saved-messages.md)
>
> The saved-messages screen in full

> [card] [Message Header](https://www.cometchat.com/docs/ui-kit/ios/v6/message-header.md)
>
> The header's options and entries

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


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