'How can we detect if user press the "Send/Return" button on keyboard with MessageKit?
I would like the user to be able to send messages when he/she presses the "Send" button on the keyboard also. For a textField I know that can be achieved with the delagte. But could anyone please point me how can we do this in MessageKit? I was not able to find a proper method to use here. Thanks in advance!
Solution 1:[1]
A detailed explanation can be found in the example project on the MessageKit repo.
The code snippet for implemeting sending the message on a Send button tap, can be seen below:
import UIKit
import MessageKit
import InputBarAccessoryView
class MyViewController: MessagesViewController {
override func viewDidLoad() {
super.viewDidLoad()
messageInputBar.delegate = self //set the delegate to receive notifications from the `InputBarAccessoryView`
}
}
extension MyViewController: InputBarAccessoryViewDelegate {
@objc internal func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
processInputBar(messageInputBar)
}
private func processInputBar(_ inputBar: InputBarAccessoryView) {
let components = inputBar.inputTextView.components
inputBar.inputTextView.text = String()
inputBar.invalidatePlugins()
inputBar.inputTextView.resignFirstResponder() // Resign first responder for iPad split view
DispatchQueue.global(qos: .default).async {
DispatchQueue.main.async { [weak self] in
guard let self = self else {return}
self.insertMessages(components)
self.messagesCollectionView.scrollToLastItem(animated: true)
}
}
}
private func insertMessages(_ data: [Any]) {
for component in data {
if let string = component as? String {
// Create a Message type and add it the the chat messages
// let message = Message(sender: currentUser, messageId: UUID().uuidString, kind: .text(string))
}
else if let image = component as? UIImage {
let item = ImageMediaItem(image: image)
// Create a Message type and add it the the chat messages
// let message = Message(sender: currentUser, messageId: UUID().uuidString, kind: .photo(item))
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nivbp |
