CreationKit Documentation

Class Messages​View​Controller

open class MessagesViewController:
  MessageKit.MessagesViewController,
  MessageKit.MessagesDataSource,
  MessageKit.MessagesDisplayDelegate,
  MessageKit.MessagesLayoutDelegate,
  InputBarAccessoryViewDelegate,
  MessageKit.MessageCellDelegate,
  QLPreviewControllerDataSource,
  QLPreviewControllerDelegate,
  UIImagePickerControllerDelegate,
  UINavigationControllerDelegate,
  UICollectionViewDataSourcePrefetching  

A ViewController subclass for displaying conversation interfaces.

Prefer to use the make factory method to init.

MessagesViewController MessagesViewController UIImagePickerControllerDelegate UIImagePickerControllerDelegate MessagesViewController->UIImagePickerControllerDelegate InputBarAccessoryViewDelegate InputBarAccessoryViewDelegate MessagesViewController->InputBarAccessoryViewDelegate MessageKit.MessagesLayoutDelegate MessageKit.MessagesLayoutDelegate MessagesViewController->MessageKit.MessagesLayoutDelegate MessageKit.MessageCellDelegate MessageKit.MessageCellDelegate MessagesViewController->MessageKit.MessageCellDelegate UICollectionViewDataSourcePrefetching UICollectionViewDataSourcePrefetching MessagesViewController->UICollectionViewDataSourcePrefetching MessageKit.MessagesDisplayDelegate MessageKit.MessagesDisplayDelegate MessagesViewController->MessageKit.MessagesDisplayDelegate QLPreviewControllerDelegate QLPreviewControllerDelegate MessagesViewController->QLPreviewControllerDelegate MessageKit.MessagesViewController MessageKit.MessagesViewController MessagesViewController->MessageKit.MessagesViewController UINavigationControllerDelegate UINavigationControllerDelegate MessagesViewController->UINavigationControllerDelegate QLPreviewControllerDataSource QLPreviewControllerDataSource MessagesViewController->QLPreviewControllerDataSource MessageKit.MessagesDataSource MessageKit.MessagesDataSource MessagesViewController->MessageKit.MessagesDataSource

Nested Types

MessagesViewController.AvatarOptions

Used to enable/disable the avatar on either side.

MessagesViewController.AttachmentOptions

Used to enable/disable the attachment types you want to support for sending.

Conforms To

InputBarAccessoryViewDelegate
MessageKit.MessageCellDelegate
MessageKit.MessagesDataSource
MessageKit.MessagesDisplayDelegate
MessageKit.MessagesLayoutDelegate
MessageKit.MessagesViewController
QLPreviewControllerDataSource
QLPreviewControllerDelegate
UICollectionViewDataSourcePrefetching
UIImagePickerControllerDelegate
UINavigationControllerDelegate

Nested Type Aliases

Send​Message​Closure

public typealias SendMessageClosure = (
    _ messages: [MessageType],
    _ completion: @escaping (_ failedMessages: [MessageType]
    ) -> Void) -> Void

The closure that will be called when a message needs to be sent.

This closure will be passed an array of MessageTypes that need to be sent, and a completion closure that you should call when the messages have finished sending. Call the completion closure with the messages that failed. This tells the controller which messages didn't send and will update the UI appropriately.

Parameters

messages

An array of MessageTypes that need to be sent. This will almost always be a single message, but may become multiple in the future.

completion

The closure that you need to call with the messages that failed to send.

failed​Messages

An array of the MessageTypes that failed to send.

Initializers

init(current​Sender:​messages:​shows​Relative​Day​Sections:​title:​send​Message:​resend​Failed​Message:​)

public init(currentSender: Sender, messages: [MessageType] = [],
              showsRelativeDaySections: Bool = true,
              title: String = "Chat".localizedCapitalized,
              sendMessage: @escaping SendMessageClosure,
              resendFailedMessage: ((FailedMessage, @escaping (Bool) -> Void) -> Void)? = nil)  

Creates a new MessagesViewController.

Parameters

current​Sender Sender

The current user as a Sender.

messages [Message​Type]

An array of messages to pre-fill the controller with.

shows​Relative​Day​Sections Bool

true by default. When true, sections will be inserted to split the messages by day.

title String

The title to display in the navigation bar.

send​Message @escaping Send​Message​Closure

The closure that will be called when a message needs to be sent. This will send you an array of MessageTypes that you should send. Call the completion closure with the failed messages to update the UI. You should only call the completion once. Called on a background thread.

resend​Failed​Message ((Failed​Message, @escaping (Bool) -> Void) -> Void)?

The closure that will be called when a failed message needs to be re-sent. Ensure to call the completion closure passing true if the message sent successfully, or false if it failed to send. You should only call the completion closure once. This closure will call the completion block with false by default. Called on a background thread.

init?(coder:​)

@available(*, unavailable, message: "MessagesViewController does not support NSCoding")
  public required init?(coder: NSCoder)  

MessagesViewController does not support NSCoding.

Properties

placeholder

public var placeholder: String = "Type your message here..."  

The placeholder to display in the input bar.

allow​Sending

public var allowSending: Bool = true  

Set to false if you want to disable text input and sending

enabled​Avatars

public var enabledAvatars: AvatarOptions = [] 

Set the avatars you want displayed.

Pass [] if you want none enabled. By default, none are shown.

supported​Attachments

public var supportedAttachments: Set<AttachmentOptions> = [] 

Set the attachment types you'd like to support.

Pass [] if you want non enabled. By default, none are supported.

shows​Relative​Day​Sections

public var showsRelativeDaySections: Bool = true

If true, the controller will automatically insert Relative Day sections (Today, Yesterday)

header

public private(set) var header: Component<MessagesHeaderLayout>? 

avatar​For​Message

public var avatarForMessage: (MessageType, @escaping (AvatarConfiguration?) -> Void) -> Void  

Called when the controller needs an Avatar for a message. Use the completion block to send back the Avatar. You can send back nil if you prefer to use the default placeholder image. Called on a background thread.

resend​Failed​Message

public var resendFailedMessage: (FailedMessage, @escaping (Bool) -> Void) -> Void  

Called when the controller attempts to resend a failed message. Use this to send the message and call the completion closure when it's re-sent. If it fails to resend, still call the completion closure, but pass false.

prefetch​Older​Messages

public var prefetchOlderMessages: (_ completion: @escaping ([MessageType]) -> Void) -> Void  

A closure type that will be called when the user scrolls to the top of the list and the controller wants to load more messages. You should set this closure if you need to support backward pagination.

When this is called, you should fetch the messages that should be inserted, then call the completion block with the MessageTypes to insert into the list.

did​Scroll

public var didScroll: ((_ scrollView: UIScrollView) -> Void)? 

did​End​Decelerating

public var didEndDecelerating: ((_ scrollView: UIScrollView) -> Void)? 

cell​For​Item​AtIndex​Path

public var cellForItemAtIndexPath: ((_ collectionView: UICollectionView, _ indexPath: IndexPath) -> Void)? 

This will be called when the delegate method collectionView(_ collectionView:cellForItemAt:) is called before the cell is returned by a call to the super implementation of this method.

load​Image

public var loadImage: ((_ url: URL, _ completion: @escaping ((UIImage) -> Void)) -> Void)? 

This will be called when the controller need to load an image from a URL for a message.

This can be ignored if you're not using ImageMessages that load from a URL.

will​Display​Item

public var willDisplayItem: ((_ message: MessageType) -> Void)? 

This will be called when the delegate method collectionView(_ collectionView:willDisplay:forItemAt:) is called You can use this method for doing something like sending an updated read status to the API.

tappable​Handler

public var tappableHandler: ((_ tappableValue: String) -> Void)? 

This will be called for messages of kind .attributedString that contain a tappable attribute.

download​Attachment

public var downloadAttachment: ((MessageAttachment, @escaping (Data, UINavigationController) -> Void) -> Void)? 

This will be called for messages that contain a MessageAttachment. You should download the attachment and present it.

preferred​Status​Bar​Style

public override var preferredStatusBarStyle: UIStatusBarStyle  

is​Last​Section​Visible

var isLastSectionVisible: Bool  

true if the last section is visible, otherwise false.

Methods

make(current​Sender:​messages:​shows​Relative​Day​Sections:​title:​send​Message:​resend​Failed​Message:​)

public static func make(currentSender: Sender, messages: [MessageType] = [],
                          showsRelativeDaySections: Bool = true,
                          title: String = "Chat".localizedCapitalized,
                          sendMessage: @escaping SendMessageClosure,
                          resendFailedMessage: ((FailedMessage, @escaping (Bool) -> Void) -> Void)? = nil) -> MessagesViewController  

Creates a new MessagesViewController.

Parameters

current​Sender Sender

The current user as a Sender.

messages [Message​Type]

An array of messages to pre-fill the controller with.

shows​Relative​Day​Sections Bool

true by default. When true, sections will be inserted to split the messages by day.

title String

The title to display in the navigation bar.

send​Message @escaping Send​Message​Closure

The closure that will be called when a message needs to be sent. This will send you an array of MessageTypes that you should send. Call the completion closure with the failed messages to update the UI. You should only call the completion once. Called on a background thread.

resend​Failed​Message ((Failed​Message, @escaping (Bool) -> Void) -> Void)?

The closure that will be called when a failed message needs to be re-sent. Ensure to call the completion closure passing true if the message sent successfully, or false if it failed to send. You should only call the completion closure once. This closure will call the completion block with false by default. Called on a background thread.

view​Did​Load()

open override func viewDidLoad()  

view​Will​Appear(_:​)

open override func viewWillAppear(_ animated: Bool)  

view​Did​Appear(_:​)

open override func viewDidAppear(_ animated: Bool)  

trait​Collection​Did​Change(_:​)

open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)  

scroll​View​Did​Scroll(_:​)

open func scrollViewDidScroll(_ scrollView: UIScrollView)  

scroll​View​Did​End​Decelerating(_:​)

open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)  

collection​View(_:​cell​For​Item​At:​)

open override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell  

collection​View(_:​will​Display:​for​Item​At:​)

open override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath)  

did​Tap​Message(in:​)

open func didTapMessage(in cell: MessageCollectionViewCell)  

gesture​Recognizer(_:​should​Recognize​Simultaneously​With:​)

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool  

add​Header(configuration:​)

@discardableResult
  func addHeader(configuration: MessagesHeaderLayout.Configuration) -> Component<MessagesHeaderLayout>  

Adds a header to the controller.

Ensure you only call this once. It's a programmer error to call this multiple times.

add​Conversation​Starter(config:​)

func addConversationStarter(config: ConversationStarter.Configuration)  

Adds a ConversationStarter to the controller.

You should call this method when you create the controller if you need it. It's safe to call this even if you know there are messages in the thread. If you instantiate this controller with messages, the controller will not show this. The controller will also hide this once messages do populate, either if you add messages yourself or if the user sends a message.

It's also safe to call this even when using an availability banner.

add​Availability​Banner(title:​text:​)

func addAvailabilityBanner(title: String, text: String? = nil)  

Adds an AvailabilityBanner to the controller.

You should call this method the second you know the receiver is unavailable if you need to show an availability banner; ideally you'd know if the receiver is unavailable before creating this controller. It's safe to call this method even if you previously called addConversationStarter(config:). The controller will manage the availability banner and conversation starter appropriately.

remove​Overlays()

func removeOverlays()  

Removes overlays such as the conversation starter and the availability banner. This will be called when a message is asked to send.

show​Attachment​Sheet()

func showAttachmentSheet()  

Displays the attachment sheet. If the supportedAttachments property is empty, this will do nothing.

show​Camera​OrPhoto​Library​Sheet()

func showCameraOrPhotoLibrarySheet()  

insert​Message(_:​)

func insertMessage(_ message: MessageType)  

Inserts a new message into the controller.

You should call this method when you've received a message and need to insert it into the UI. The controller also uses this method to insert sent messages into the UI.

Parameters

message Message​Type

The MessageType to insert into the collection.

prepend​Messages(_:​)

func prependMessages(_ messages: [MessageType])  

current​Sender()

func currentSender() -> SenderType  

The SenderType object that is currently set as the sender for this instance.

Returns

The current sender.

number​OfSections(in:​)

func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int  

message​For​Item(at:​in:​)

func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType  

message​Timestamp​Label​Attributed​Text(for:​at:​)

func messageTimestampLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?  

message​Top​Label​Attributed​Text(for:​at:​)

func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?  

message​Bottom​Label​Attributed​Text(for:​at:​)

func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?  

message​Top​Label​Height(for:​at:​in:​)

func messageTopLabelHeight(
    for message: MessageType,
    at indexPath: IndexPath,
    in messagesCollectionView: MessagesCollectionView
  ) -> CGFloat  

message​Bottom​Label​Height(for:​at:​in:​)

func messageBottomLabelHeight(
    for message: MessageType,
    at indexPath: IndexPath,
    in messagesCollectionView: MessagesCollectionView
  ) -> CGFloat  

configure​Media​Message​Image​View(_:​for:​at:​in:​)

func configureMediaMessageImageView(
    _ imageView: UIImageView,
    for message: MessageType,
    at indexPath: IndexPath,
    in messagesCollectionView: MessagesCollectionView
  )  

custom​Cell(for:​at:​in:​)

func customCell(
    for message: MessageType,
    at indexPath: IndexPath,
    in messagesCollectionView: MessagesCollectionView
  ) -> UICollectionViewCell  

collection​View(_:​prefetch​Items​At:​)

open func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath])  

input​Bar(_:​did​Press​Send​Button​With:​)

@objc func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String)  

process​Input​Bar(_:​)

func processInputBar(_ inputBar: InputBarAccessoryView)  

configure​Avatar​View(_:​for:​at:​in:​)

func configureAvatarView(
    _ avatarView: MessageAvatar,
    for message: MessageType,
    at indexPath: IndexPath,
    in messagesCollectionView: MessagesCollectionView
  )  

did​Tap​Image(in:​)

func didTapImage(in cell: MessageCollectionViewCell)  

did​Tap​Message​Bottom​Label(in:​)

func didTapMessageBottomLabel(in cell: MessageCollectionViewCell)  

background​Color(for:​at:​in:​)

func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor  

text​Color(for:​at:​in:​)

func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor  

message​Style(for:​at:​in:​)

func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle  

number​OfPreview​Items(in:​)

public func numberOfPreviewItems(in controller: QLPreviewController) -> Int  

preview​Controller(_:​preview​Item​At:​)

public func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem  

preview​Controller(_:​transition​View​For:​)

public func previewController(_ controller: QLPreviewController, transitionViewFor item: QLPreviewItem) -> UIView?