Skip to main content
CometChatCreatePoll is the form for a new poll: a question and its options. The message composer opens it from its Poll attachment option and posts the poll for you. You can also host it yourself. The form is not a connected component. It makes no network call: it hands the question and options back when Send is tapped, and its host creates the poll and tells it how that went.
CometChatCreatePoll showing a question and two answers

Where It Fits

CometChatCreatePoll is a View.

Quick Start

With the Composer

The composer’s Poll option is hidden by default. Show it and the composer does the rest.
The composer opens the form in a sheet and posts the poll to the chat — quoted, if you are replying in this conversation. While it posts, the form shows “Creating Poll…” and the sheet cannot be dismissed. On success the sheet closes, and the poll arrives in the message list like any other message. A failure shows its message under the form and does not reach the composer’s onError.The option is dropped inside a thread (see the composer’s menu caveat).
Polls need the Polls extension enabled on your CometChat dashboard.

Host It Yourself

Handle the submit callback, post the poll, and tell the form how it went. The composer’s store has the call it uses.
Handle onSubmitClick, post the poll, and pass isSubmitting and errorMessage back in.
createPoll posts to the store’s current user or group. With neither set, it fails with ERR_NO_RECEIVER.

The Form

The form has a navigation bar with Cancel, the title and Send, then a Question section and a Set the answers section. The form takes no focus by itself.
The SwiftUI create-poll form filled in: Cancel, Poll and Send in the bar, a lunch question, two answers and an empty Add row that appeared once both were filled
  • The question is multi-line. Each option is a single line.
  • Two option rows to start. When both are filled, a third appears. From then on, typing in the last row adds another, up to 12. At 12 there is no empty row.
  • Removing: there is no delete button. An empty row is removed when it loses focus, unless it is the last row or only two rows are left. A row of spaces counts as blank and is removed.
  • Reordering: long-press a filled row and drag it onto another filled row. The drag handle shows on filled rows only.
  • The form scrolls to the bottom whenever rows are added or removed.
  • A row’s focus belongs to its position, not its content. When an empty row loses focus to the row below it and is removed, the focus moves to the next row down. For example, tapping D in A, (empty), C, D, (empty) leaves the caret in the last, empty row.
Send is enabled when the question is filled and the first two rows are filled — those two rows, not any two. It is re-checked on every change. The form does not reset after a send; the host decides.onSubmitClick receives the question, trimmed, and one entry per row at the row’s index: the row’s text, trimmed, or nil for a blank row. So A, blank, C arrives as ["A", nil, "C"] — and the composer posts it that way, as a JSON null between the two options.

Actions and Events

Callback Methods


Functionality


Advanced Methods

CometChatCreatePoll is a View struct. Customise it with its parameters and style.

Accessibility

In the SwiftUI toolkit:
  • The question, each option and each drag handle have VoiceOver labels. Send says whether it is disabled.
  • The “Creating Poll…” alert has its own VoiceOver label.

Style

CometChatCreatePollStyle is sparse: set only what you change, and everything else follows the theme.

Next Steps

Message Composer

Hosts this form behind the Poll option

Sticker Keyboard

The composer’s other hosted component