0

I'm learning Swift and trying to get MessageKit up and running. I'v followed the getting started guide but keeping getting the error: Type 'ChatViewController' does not conform to protocol 'MessagesDataSource'

Here's my code:

import UIKit
import MessageKit

class ChatViewController: MessagesViewController {

let sender = Sender(id: "any_unique_id", displayName: "Steven")
let messages: [MessageType] = []

override func viewDidLoad() {
    super.viewDidLoad()
    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messagesLayoutDelegate = self
    messagesCollectionView.messagesDisplayDelegate = self
  }
}
extension ChatViewController: MessagesDataSource {

func currentSender() -> Sender {
    return Sender(id: "any_unique_id", displayName: "Steven")
}

func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
    return messages.count
}

func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
    return messages[indexPath.section]
  }
}

extension ChatViewController: MessagesDisplayDelegate, MessagesLayoutDelegate {}

Any help would be appreciated. Thank you

Booysenberry
  • 223
  • 3
  • 16
  • 1
    Right click on the ChatViewController extension which conforms to the protocol and select Refactor -> Add Missing Protocol Requirements from the menu. It will fill out the methods which you might not have implemented yet. – ZeMoon Feb 28 '19 at 03:57
  • Thanks. Tried that already but no joy. – Booysenberry Feb 28 '19 at 03:59
  • From what I see in the documentation, you have implemented only 3 of the 9 methods for MessagesDataSource. You need to implement the other 6 methods as well in order to conform to this protocol. – ZeMoon Feb 28 '19 at 04:15
  • According to the getting started guide: "You must implement the following 3 methods to conform to MessagesDataSource:" – Booysenberry Feb 28 '19 at 04:20
  • You're right, the other 6 methods have default implementations. I see you have copied the same implementation as in the guide. Here's what you do: Delete the current methods as you have written in the extension and use the Refactor option to let Xcode fill out these methods for you. Then see the difference. – ZeMoon Feb 28 '19 at 04:57
  • Tried that as well. Same error message. – Booysenberry Feb 28 '19 at 05:25
  • I installed MessageKit with cocapod and your example code is working perfectly. Maybe you should give a try. https://guides.cocoapods.org/using/using-cocoapods.html – Gyuri T Feb 28 '19 at 10:41

2 Answers2

1
import UIKit 
import MessageKit 
import MessageInputBar

class ChatViewController: MessagesViewController {

    private var messages: [Message] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }

    private func setup() {

        messageInputBar.sendButton.setTitleColor(.white, for: .disabled)
        messageInputBar.sendButton.setTitleColor(.white, for: .normal)
        messageInputBar.sendButton.setTitleColor(.white, for: .highlighted)
        messageInputBar.sendButton.setTitle(strings.send, for: .normal)
        messageInputBar.inputTextView.tintColor = .white
        messageInputBar.inputTextView.placeholderTextColor = .white
        messageInputBar.inputTextView.textColor = .white
        messageInputBar.inputTextView.placeholder = "placeholder"
        messagesCollectionView.backgroundColor = .white
        messageInputBar.backgroundView.backgroundColor = .blue


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

        scrollsToBottomOnKeyboardBeginsEditing = true // default false
        maintainPositionOnKeyboardFrameChanged = true // default false

    } 
}


extension ChatViewController: MessagesDataSource, MessageCellDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {

    func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        return messages.count
    }

    func currentSender() -> Sender {
        return Sender(id: member.messageID, displayName: member.name)
    }

    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        return messages[indexPath.section]
    }

    func didTapAvatar(in cell: MessageCollectionViewCell) {
        guard let indexPath = messagesCollectionView.indexPath(for: cell) else { return }
        let message = messages[indexPath.section]
        print("Message : \(message)")
    }

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

}

extension ChatViewController: MessagesLayoutDelegate {
    func heightForLocation(message: MessageType, at indexPath: IndexPath, with maxWidth: CGFloat, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
        return 0
    } 
}

extension ChatViewController: MessagesDisplayDelegate {

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

        let message = messages[indexPath.section]
        if let userimage = URL(string: message.member.image) {
            //Nuke.loadImage(with: userimage, options: config.options_stream, into: avatarView)
            print("Image URL : \(userimage)")
        }
        avatarView.backgroundColor = .black
    }

    func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
        let tail: MessageStyle.TailCorner = isFromCurrentSender(message: message) ? .bottomRight : .bottomLeft
        return .bubbleTail(tail, .curved)
    } 
}

extension ChatViewController: MessageInputBarDelegate {

    func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
        print("Message : \(text)")
    }
}
istorry
  • 48
  • 6
0

you have missed senderType protocol so try this,

struct sender: SenderType{
    var senderId: String
    var displayName: String
}
Lihour
  • 1