Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 864
Awais Fayyaz Avatar asked Aug 15 '18 11:08

Awais Fayyaz


2 Answers

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

like image 126
yoninja Avatar answered Oct 23 '22 13:10

yoninja


Checkout the Example in the Library

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

like image 1
Dan Leonard Avatar answered Oct 23 '22 13:10

Dan Leonard