I am building a messaging app using JSQMessagesViewController and I was wondering if there would be a way for me to require a user's text input to be between 10 and 140 characters in length.
I have put this following piece of code
if (text.characters.count <= 10 || text.characters.count >= 140) {
self.inputToolbar.sendButtonOnRight = false
}
else {
self.inputToolbar.sendButtonOnRight = true
}
in several key spots such as the function didPressSendButton
:
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!,
senderDisplayName: String!, date: NSDate!) {
let itemRef = messageRef.childByAutoId() // 1
let messageItem = [ // 2
"text": text,
"senderId": senderId,
"location": getLocation()
]
itemRef.setValue(messageItem) // 3
// 4
JSQSystemSoundPlayer.jsq_playMessageSentSound()
// 5
finishSendingMessage()
Answers.logCustomEventWithName("Message sent", customAttributes: nil)
}
and my addMessages
function:
func addMessage(id: String, text: String, displayName: String) {
let message = JSQMessage(senderId: id, displayName: displayName, text: text)
messages.append(message)
}
like such:
func addMessage(id: String, text: String, displayName: String) {
if (text.characters.count <= 10 || text.characters.count >= 140) {
self.inputToolbar.sendButtonOnRight = false
}
else {
self.inputToolbar.sendButtonOnRight = true
let message = JSQMessage(senderId: id, displayName: displayName, text: text)
messages.append(message)
}
}
However that does not work.
What kind of works for me is doing this in my addMessages function:
if (text.characters.count <= 10 || text.characters.count >= 140) {
// pass
}
else {
let message = JSQMessage(senderId: id, displayName: displayName, text: text)
messages.append(message)
}
In this case, the character filter works but the send button is still enabled, so the data is sent to my database, which I do not want. It would work a lot better for me if the send button was just disabled entirely if the text input is not long enough or too long.
If anybody would be able to help me with this, that would be great. Thanks!