How to properly implement MessageKit in swift 4? | Delegate functions are not being called
Asked Answered
R

2

11

I was trying to implement MessageKit (Replacement of JSQMessagesViewController) for chat feature, in my iOS swift application. I have followed this raywenderlich tutorial. I installed using Pods and followed this tutorial step by step. My messages are showing. According to this tutorial, for hiding the user avatar, i set my viewController to be delegate of messagesLayoutDelegate and messagesDisplayDelegate.

//Inside view did load, i set the delegates, along with data source
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self

However, the delegate functions of MessagesLayoutDelegate are not being called.

I wanted to hide user avatar so i adopted the protocols in my viewController

extension ChatLogVC_MessageKit: MessagesLayoutDelegate {

 func avatarSize(for message: MessageType, at indexPath: IndexPath,
              in messagesCollectionView: MessagesCollectionView) -> CGSize {

    //return zero size for hiding avatar, but this function is not being called. 

  return .zero
 }

 func footerViewSize(for message: MessageType, at indexPath: IndexPath,
                      in messagesCollectionView: MessagesCollectionView) -> CGSize {

    //also not being called

    return CGSize(width: 0, height: 8)
 }

}

I have set break points inside these functions but the break points never hit. Morever, the shouldDisplayHeader function of MessagesDisplayDelegate is also not being called

Any help would be much appreciated

Note: I have tested it on simulator only.

Reconstitute answered 15/8, 2018 at 11:22 Comment(1)
scaledrone.com/blog/ios-chat-tutorialQuiescent
V
4

Your delegate methods look outdated. If you are using Cocoa pods, at least use the 1.0.0 beta 1 version:

pod 'MessageKit', '1.0.0-beta.1'

Or, better, use the 1.0.0 version:

pod 'MessageKit', '1.0.0'

In your viewDidLoad() method set the delegate:

messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self
messagesCollectionView.messageCellDelegate = self

By the way, the avatar size is set in the MessagesDisplayDelegate method func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView).

Vernellvernen answered 31/8, 2018 at 5:52 Comment(1)
a sample controller that uses MessageKitKidder
E
1

Checkout the Example in the Library

https://github.com/MessageKit/MessageKit/tree/master/Example

Effy answered 12/10, 2018 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.