我需要在 Swift 中不使用 FireBase 来创建MessageKit聊天应用程序吗?
提前致谢。
这是您可以执行的操作:
MyMessagesViewController
并从MessagesViewController
MessageKit 扩展它。final class MyMessagesViewController: MessagesViewController {
...
}
InputBarAccessoryView
扩展,覆盖override func setup()
并设置您的子视图和布局。final class MyInputBarAccessoryView: InputBarAccessoryView {
override func setup() {
super.setup()
// All your setup here
}
}
并在MyMessagesViewController
创建messageInputBar
:
required init(...) {
messageInputBar = MyInputBarAccessoryView()
}
override var canBecomeFirstResponder: Bool {
return true
}
override var inputAccessoryView: UIView? {
return messageInputBar
}
MessagesCollectionViewFlowLayout
,覆盖cellSizeCalculatorForItem(at:)
并创建你自己的CellSizeCalculator
,覆盖为每条新消息messageContainerSize(for:)
返回一个。CGSize
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
}
}
MyMessagesViewController
viewDidLoad 方法中,MessageCollectionView
为单元格创建、注册 nib 并设置委托。override func viewDidLoad() {
super.viewDidLoad()
messageCollectionView = MessageCollectionView(
frame: .zero,
collectionViewLayout: MyCollectionViewFlowLayout()
)
// Common setups here
// delegates
messagesCollectionView.messagesDataSource = self
}
MessagesDataSourceDelegate
方法上,实现currentSender()
并返回您的 OWN currentSender,就像您在使用 Firebase 时必须做的那样,但现在您必须使用服务器发件人 ID 或类似的东西。当您创建自己的自定义单元格时,customCell(for:, at:, in:)
请正确实施并出列您已注册的单元格。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
}
它是 MessageKit 自定义单元的基本实现。显然你想实现更多的东西。
我希望它可以帮助你:)
消息工具包根本不绑定到 Firebase,您可以使用任何您想要的数据库。