Nice tutorial so far. Iāve built a one to one chat based on this tutorial with user authentication. The way I track a conversation between two users is I sort both their uid, then I truncate each one of them to take just 10 characters then I merge them.
I chose this approach because I canāt rely on the Id generated by with chilldByAutoId() method and I donāt want to store all these ids and write a lot of logic in order to know which conversation goes with which users.
The chat is working so far but I wonder if there isnāt a better way of doing this?
Awesome tutorial! One thing you might be able to add for those who are feeling extra adventurous is how to implement the didPressAccessoryButton method. As of now the app just crashes if you press that little paper clip. Could we bring up an ImagePickerController to send a photo? What if we wanted to send a GIF? Does the JSQ Messages View Controller support these things? Thanks!
Remove print(authData) in the ChatViewController.swift file to simulate and install the build on your iPhone.
While the ChatChat app builds on the XCode iPhone simulator successfully, there was an issue in the main thread while trying to run it on my iPhone to test out the messaging function on both the XCode simulator and the iOS device I was trying to test it on.
I am trying to do the same as you and build in a one to one messaging feature with my app.
I am also trying to gather UIDās to make sure the messages are going to the right two people. Is there any tutorials on this as I am a bit stuck. I think Iāll use the childbyautoid to create a key in a messages node. Within this key I was then going to have āto: uid ( of person message is going to)ā, āmessage: contentā, and then maybe a āfrom: senderā.
I think I get the tutorial bit late ā¦ you think you can upgrade to FirebaseDatabase (3.0.1)? because I have problems with observeMessages()
Thanks and regaras.
Thank you! excellent works, the only thing missing is me add the label name, because my app will have a group chat, try the following code that previously worked me but not anymore.
override func collectionView(collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> NSAttributedString! {
let data = self.collectionView(self.collectionView, messageDataForItemAtIndexPath: indexPath)
if (self.senderDisplayName == data.senderDisplayName()) {
return nil
}
return NSAttributedString(string: data.senderDisplayName())
}
override func collectionView(collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
let data = self.collectionView(self.collectionView, messageDataForItemAtIndexPath: indexPath)
if (self.senderDisplayName == data.senderDisplayName()) {
return 0.0
}
return kJSQMessagesCollectionViewCellLabelHeightDefault
}
Edit: I needed to add func displayName addMessage. Now if you show me the label of the sender.
let rootReference = FIRDatabase.database().referenceFromURL("https://sweltering-heat-3417.firebaseio.com")
var messageReference: FIRDatabaseReference!
messageReference = rootReference.child("messages")
snapshot becomes of type FIRDataSnapshot
There is a problem that Iām trying to solve: in FIRAuth docs the method signInAnonymouslyWithCompletion returns the currentUser instance. Yet when I call .displayName it returns nil.
This is why I hardcoded senderId property of chatViewController - it must not be nil.
Hope weāll work it out, the tutorial is simple and excellent.
the FIRAuth method runs on a separate thread from the main thread so if your calling .displayName just after the completion block it may not be populated yet because the completion block has not finished loading yet. try putting a variable at class level that is optional and of the type of data you want to put in it. In this case, String. Inside the completion block set the variable to whatever you want to from the snapshot. Do this in viewdidLoad. Then in viewdidappear the variable should have the info you need and you can work with it from then on. The other option is a second closure that takes the FIRAuth completion as a parameter, ensuring that you have the results from the FIRAuth completion before the other closure begins.