0

I'm attempting to implement a simple messaging application but the MessageInputBarDelegate methods are not firing. Other extensions such as MessagesDisplayDelegate appear to fire as expected.

I'm using Swift 4.4.2

pod file :

use_frameworks!

target 'm-v0.01' do
    pod 'Socket.IO-Client-Swift', '~> 15.1.0'
    pod 'Alamofire', '~> 5.0.0-beta.5'
    pod 'MessageKit'
#    pod 'MessageInputBar'
#    pod 'MessageInputBar', :git => 'https://github.com/MessageKit/MessageInputBar.git', :branch => 'master'
end 

I have two controllers, one for login and one for displaying messages :

ViewController : (for login)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var loginValue: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.destination is MViewController
        {
            let vc = segue.destination as? MViewController
            vc?.text = loginValue.text!
        }
    }

    @IBAction func loginAction(_ sender: UIButton!) {

        print("Entered Text" , loginValue.text)



        performSegue(withIdentifier: "view2", sender: self)

    }

}

MViewController (for displaying messages) :

import UIKit
import SocketIO
import Alamofire
import MessageKit

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

        return 0
    }
}

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

        let message = messages[indexPath.section]
        let color = message.member.color
        avatarView.backgroundColor = color

        print("MessagesDisplayDelegate")
    }
}

extension MViewController: MessageCellDelegate {

    func didTapAvatar(in cell: MessageCollectionViewCell) {
        print("Avatar tapped")
    }

    func didTapMessage(in cell: MessageCollectionViewCell) {
        print("Message tapped")

    }

    func didTapCellTopLabel(in cell: MessageCollectionViewCell) {
        print("Top cell label tapped")
    }

    func didTapMessageTopLabel(in cell: MessageCollectionViewCell) {
        print("Top message label tapped")
    }

    func didTapMessageBottomLabel(in cell: MessageCollectionViewCell) {
        print("Bottom label tapped")
    }

    func didTapAccessoryView(in cell: MessageCollectionViewCell) {
        print("Accessory view tapped")
    }

}


extension MViewController: MessageInputBarDelegate {

    func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
        print("here")
    }

    func messageInputBar(_ inputBar: MessageInputBar, textViewTextDidChangeTo text: String) {
        print("changed")
    }

}

extension MViewController: MessagesDataSource {

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

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

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

        return messages[indexPath.section]
    }

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

        return 12
    }

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

        return NSAttributedString(
            string: message.sender.displayName,
            attributes: [.font: UIFont.systemFont(ofSize: 12)])
    }
}

class MViewController: MessagesViewController{

    var text:String = ""

    var messages: [Message] = []
    var member: Member!

    override func viewDidLoad() {
        super.viewDidLoad()
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self
        messagesCollectionView.messageCellDelegate = self
        messageInputBar.delegate = self
    }

}

I looked at many tutorials and docs and this is consistently documented as accessing the methods that fire when a user hits send or changes text :

func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
    print("here")
}

func messageInputBar(_ inputBar: MessageInputBar, textViewTextDidChangeTo text: String) {
    print("changed")
}

Have I omitted something in how I'm sending messages that is preventing the message listener being fired?

There is a pod library 'MessageInputBar' , should I leverage this also ? It appears I do not need this as the code compiles ?

Update :

I've tried setting the delegate on messagesCollectionView :

 messagesCollectionView.delegate = self as MessageInputBarDelegate as? UICollectionViewDelegate

But same result : the send event is not firing.

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • where is your code that sets controller as `MessageInputBarDelegate` delegate? – Lu_ Jun 25 '19 at 15:13
  • @Lu_ I don't think I've set that. I'm unsure how to set it. Can provide steps ? Should it be set from 'ViewController' within the 'prepare' function ? – blue-sky Jun 25 '19 at 16:12
  • @Lu_ is the delegate not being set here : " extension MViewController: MessageInputBarDelegate {" ? – blue-sky Jun 25 '19 at 17:35

2 Answers2

0

From the info in comments I assume you don't have an outlet to your message bar and you don't have a delegate set to your viewController. Try to se it up in viewDidLoad:

messageInputBar.delegate = self

And for that, your controller have to subclass a MessagesViewController, so in your situation it will be changing

class ViewController: UIViewController {

to:

class ViewController: MessagesViewController {
Lu_
  • 2,577
  • 16
  • 24
  • thanks but this is not correct as MViewController (not ViewController) contains the message input bar so I need to subclass MessagesViewController in MViewController which I have done. – blue-sky Jun 26 '19 at 06:29
0

I needed to use alternate MessageKit version :

pod 'MessageKit' , '1.0.0'
blue-sky
  • 51,962
  • 152
  • 427
  • 752