[英]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.对于 textField,我知道可以通过 delagte 来实现。 But could anyone please point me how can we do this in MessageKit?但是谁能指出我如何在 MessageKit 中做到这一点? I was not able to find a proper method to use here.我无法在这里找到合适的方法。 Thanks in advance!提前致谢!
A detailed explanation can be found in the example project on the MessageKit
repo.可以在MessageKit
库的示例项目中找到详细说明。
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))
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.