我尝试过来自各种链接的代码,但不知何故,最新的SDK似乎什么都不起作用。我正在尝试以下代码:
let message = QBChatMessage()
message.senderID = (self.senderID)
message.dialogID = self.chatDialogModel.id
message.dateSent = Date()
message.readIDs = [NSNumber.init(value: (self.senderID))]
message.deliveredIDs = [NSNumber.init(value: (self.senderID))]
let aURL = Bundle.main.url(forResource: "testVideo", withExtension:"mp4")
let aData = try! Data(contentsOf: aURL!)
QBRequest.tUploadFile(aData, fileName: "Attachment Video",
contentType:"video/mp4",
isPublic: true, successBlock: { (qbResponse, qbBlob) in
let attachment : QBChatAttachment = QBChatAttachment()
attachment.type = "video"
attachment.id = qbBlob.uid
attachment.url = qbBlob.publicUrl()
message.attachments = [attachment]
message.text = "Attachment video"
self.chatDataSource.add(message)
ServicesManager.instance().chatService.sendAttachmentMessage(message, to: (self.chatDialogModel)!, with: attachment, completion: { [weak self] (error) -> Void in
self?.attachmentCellsMap.removeObject(forKey: message.id as AnyObject?)
guard error != nil else { return }
self?.chatDataSource.delete(message)
})
}, statusBlock: nil) { (error) in
}
我找到了一个解决方案:
首先编写从照片库获取本地视频文件URL的代码。获得url后,调用此函数。
func sendVideoAsAttachment(_ url : URL){
let message = QBChatMessage()
message.senderID = (self.senderID)
message.dialogID = self.chatDialogModel.id
message.dateSent = Date()
let attachment = QBChatAttachment.videoAttachment(withFileURL: url)
self.chatDataSource.add(message)
ServicesManager.instance().chatService.sendAttachmentMessage(message, to: self.chatDialogModel, with: attachment, completion: { (error) in
self.attachmentCellsMap.removeObject(forKey: message.id as AnyObject?)
guard error != nil else { return }
self.chatDataSource.delete(message)
})
}
这将以附件形式发送视频。