我已将 MessageKit 集成到我的项目中,我使用 MySQL 作为我的数据库,使用 PHP 作为我的 API。我已经能够从数据库中提取消息并显示它们;但是发件人的姓名被替换为当前登录的用户。我已经打印了发件人姓名,并且通过应用程序正确显示是正确的,但不知何故,显示名称未显示正确的名称。这是带有以下代码的屏幕截图。我检查了 MessageKit 说明,但无法弄清楚我做错了什么。 成员.swift 文件
internal struct Message: MessageType {
var sender: SenderType
var senderId: String
var messageId: String
var member: String
var id: String?
var content: String
let sentDate: Date
var messagedata: MessageKind {
if let image = image {
return .photo((image as? MediaItem)!)
} else {
return .text(content)
}
}
var image: UIImage? = nil
var downloadURL: URL? = nil
init(messageuser: Sender, content: String) {
sender = Sender(id: meid, displayName: AppSettings.displayName)
self.senderId = meid
self.messageId = uuid
self.member = me
id = nil
self.content = content
sentDate = Date()
self.image = nil
//self.kind = (content as? MessageKind)!
}
init(messageuser: Sender, image: UIImage) {
sender = Sender(id: meid, displayName: AppSettings.displayName)
self.senderId = meid
self.messageId = uuid
self.member = me
id = nil
self.content = ""
sentDate = Date()
self.image = image
}
}
extension Message: Comparable {
static func == (lhs: Message, rhs: Message) -> Bool {
return lhs.id == rhs.id
}
static func < (lhs: Message, rhs: Message) -> Bool {
return lhs.id == rhs.id
}
var kind: MessageKind {
return .text(content)
}
}
DirectMessageViewController
func loadMessages(offset: Int, limit: Int) {
isLoading = true
let me = user!["username"] as! String
let meid = user!["id"] as! String
let uuid = getmessages["uuid"] as! String
print("tell me what the uuid is", uuid)
let url = URL(string: "https://localhost/message.php")
let body = "username=\(me)&uuid=\(uuid)&recipient_id=\(meid)&limit=\(limit)&offset=\(offset)"
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
if error != nil {
Helper().showAlert(title: "Server Error", message: error!.localizedDescription, in: self)
return
}
do {
guard let data = data else {
Helper().showAlert(title: "Data Error", message: error!.localizedDescription, in: self)
self.isLoading = false
return
}
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary
guard let parseJSON = json else {
print("Error while parsing")
return
}
guard let posts = parseJSON["messages"] as? [NSDictionary] else {
self.isLoading = false
print("Error while parseJSONing")
return
}
self.hhmessages = posts as! [AnyObject?]
for i in 0 ..< self.hhmessages.count {
let sender = Sender(id: (self.hhmessages[i]?["sender_id"])! as! String, displayName: (self.hhmessages[i]?["sender"])! as! String)
let member = self.hhmessages[i]?["recipient"]
print("who's talking", sender as Any)
let text = self.hhmessages[i]?["messagetext"] as? String
let uuid = self.hhmessages[i]?["uuid"]
let sid = self.hhmessages[i]?["sender_id"]
let message_id = self.hhmessages[i]?["notesid"] as? String
let date = self.hhmessages[i]?["date"] as? Date
let message = Message(messageuser: sender, content: text!)
self.messages.append(message)
self.messageList = self.messages
self.messagesCollectionView.reloadData()
self.messagesCollectionView.scrollToBottom()
}
} catch {
Helper().showAlert(title: "JSON Error", message: error.localizedDescription, in: self)
self.isLoading = false
return
}
}
}.resume()
}
}
extension DirectMessageViewController: MessagesDataSource {
func numberOfSections(
in messagesCollectionView: MessagesCollectionView) -> Int {
return messages.count
}
func currentSender() -> SenderType {
return Sender(id: (sender_id as? String)!, displayName: sender.displayName)
}
messageTopLabelAttributedText
func messageTopLabelAttributedText(
for message: MessageType,
at indexPath: IndexPath) -> NSAttributedString? {
return NSAttributedString(
string: message.sender.displayName,
attributes: [.font: UIFont.systemFont(ofSize: 12)])
}
}