-2

I need to create MessageKit Chat app without using FireBase in Swift?

Thanks in Advance.

Sreekanth G
  • 658
  • 6
  • 13

2 Answers2

1

Message kit is not tied to Firebase at all you can use any database you would like.

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32
1

Here is what you can do:

  1. Create a MyMessagesViewController and extends it from MessagesViewController MessageKit.
final class MyMessagesViewController: MessagesViewController {
   ...
}
  1. Create your own InputBarAccessoryView extending from it, overriding override func setup() and set your subviews and layout up.
final class MyInputBarAccessoryView: InputBarAccessoryView {
   override func setup() {
      super.setup()
      // All your setup here
   }
}

And in MyMessagesViewController create the messageInputBar:

required init(...) {
   messageInputBar = MyInputBarAccessoryView()
}

override var canBecomeFirstResponder: Bool {
   return true
}

override var inputAccessoryView: UIView? {
   return messageInputBar
}
  1. Create your own MessagesCollectionViewFlowLayout, overriding cellSizeCalculatorForItem(at:) and create your own CellSizeCalculator, overriding messageContainerSize(for:) for returning a CGSize for each new message.
class MyMessagesCollectionViewFlowLayout: MessagesCollectionViewFlowLayout {

   private let sizeCalculator = MyMessageSizeCalculator()

   public override func cellSizeCalculatorForItem(at indexPath: IndexPath) -> CellSizeCalculator {
      if case .custom = message.kind {
         return sizeCalculator
      }

      return super.cellSizeCalculatorForItem(at: indexPath)
   }
}
class MyMessageSizeCalculator: MessageSizeCalculator {
   override func messageContainerSize(for message: MessageType) -> CGSize {
      return size // Here you have to calculate your cell size based on message
   }
}
  1. In MyMessagesViewController viewDidLoad method, create the MessageCollectionView, register nibs for cells and setup delegates.
override func viewDidLoad() {
   super.viewDidLoad()
   messageCollectionView = MessageCollectionView(
      frame: .zero,
      collectionViewLayout: MyCollectionViewFlowLayout()
   )

   // Common setups here

   // delegates
   messagesCollectionView.messagesDataSource = self
}
  1. On MessagesDataSourceDelegate methods, implement currentSender() and returns your OWN currentSender just like you had to do when you used Firebase, but now you have to use a server sender ID or something like that. As you created your own custom cell, implement customCell(for:, at:, in:) and dequeue correctly the cell that you had registered.
func currentSender() -> SenderType {
   // You have to create some model that extends from SenderType and return here okay?
   return myClientSender
}

func customCell(
   for message: MessageType,
   at indexPath: IndexPath,
   in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell {
   
   // dequeue correctly the cell based on message and indexPath
   return cell
}

It's the basic implementation for MessageKit custom cells. Obviously you would like to implement more things.

I hope it could help you :)