为了解决这个问题,我必须:
import InputBarAccessoryView
所以你不必这样做:
messageInputBar.delegate = self as InputBarAccessoryViewDelegate
并在您的 viewDidLoad() 中设置这些代表(颜色可选)
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()
}
那么如果使用实时数据库 - 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 = ""
}
}