So This is the controller hierarchy tabBarController -> some controller & chat channel controller.
and this chat channel controller is also a navigation controller. When I select row it pushes to chat controller which is of class MessageViewController.
I have 2 issues here one minor one major.
minor one is that the avatar.
func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
print("delegate method called")
return .zero
}
I set it to zero in one of the delegates but it still shows.And also it never prints the statement.
major one is that the input bar is not showing at all. I already have InputBarAccessoryView in my pod file and
messageInputBar.delegate = self as InputBarAccessoryViewDelegate
extension ChatViewController: InputBarAccessoryViewDelegate {
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
print("text")
}
}
but nothing shows. I also checked the views and I couldn't find it
to resolve this I had to :
import InputBarAccessoryView
so you dont have to do:
messageInputBar.delegate = self as InputBarAccessoryViewDelegate
and in your viewDidLoad() set these delegates(colors optional)
super.viewDidLoad()
guard let userId = Auth.auth().currentUser?.uid else{return}
Database.fetchUserWithUID(uid: userId) { (user) in
self.currentLogginUser = user
}
messageInputBar.delegate = self
maintainPositionOnKeyboardFrameChanged = true
messageInputBar.inputTextView.tintColor = .yellow
messageInputBar.sendButton.setTitleColor(.purple, for: .normal)
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self
messagesCollectionView.messageCellDelegate = self
messageInputBar.leftStackView.alignment = .center
messageInputBar.setLeftStackViewWidthConstant(to: 50, animated: false)
getMessages()
}
then if using Realtime Database - Firebase
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
guard let recipient = self.recipient else{return}
guard let uid = Auth.auth().currentUser?.uid else{return}
let ref = Database.database().reference().child("messages")
let childRef = ref.childByAutoId()
let values = ["sender": uid, "text": text, "recipient": recipient.uid, "time": Date().timeIntervalSince1970.description]
childRef.updateChildValues(values) { (err, ref) in
if let err = err{
print(err)
return
}
let userMessageRef = Database.database().reference().child("user-messages").child(uid).child(recipient.uid)
//add chat id generated from AutoId
guard let messageId = childRef.key else{return}
userMessageRef.updateChildValues([messageId: 1])
let recipientMessageRef = Database.database().reference().child("user-messages").child(recipient.uid).child(uid)
recipientMessageRef.updateChildValues([messageId:1])
}
inputBar.inputTextView.text = ""
}
}
Solution for showing the input bar:
class ViewController: MessagesViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.becomeFirstResponder()
}
}
Source: https://github.com/MessageKit/MessageKit/issues/501#issuecomment-361913168
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With