将附件发送到 QuickBlox IOS sdk

Send attachment to QuickBlox IOS sdk

我已经下载了 quickblox 示例聊天应用程序,并且可以正常发送文本消息。但是如何发送图片、视频等附件呢?

根据 Quickblox 的文档。 class 名为 QBChatAttachment,其属性为 typeurlid 但是如何附加图片、视频等文件?

请正确阅读 SimpleSample-chat users-ios 中的 link 他们提到了有关如何发送附件以及如何接收和下载附件等的所有详细信息。

要在 quickbox 中发送和接收附件,请遵循此 link Send and Receive Attachment

详细说明:

Send and receive a message with attachment Send attachment

It's possible to add attachments to message: for example, image, audio file or video file. We don't have any restrictions here - you can attach any type of file.

To send a message with attachments you should use the same way as you send regular message with text, but add to it an attachment object. Attachment can be:

1) A file content Module Example

2) A file in Custom Objects module Example

To send a message with attachment

you should upload a file to Content module, Custom Objects module using sample above or use an url to any file in Internet. Then you should incorporate an ID to file to message.

For example, we use Content module to store attachments. Next snippets show

how to upload a file to Content module and send it as an attach:

// Upload a file to the Content module
   NSData *imageData = UIImagePNGRepresentation([UIImage  imageNamed:@"arrow.png"]);

    [QBRequest TUploadFile:imageData fileName:@"arrow.png" contentType:@"image/png" isPublic:NO successBlock:^(QBResponse
*response, QBCBlob *uploadedBlob) {
NSUInteger uploadedFileID = uploadedBlob.ID;

 // Create chat message with attach
 //
 QBChatMessage *message = [QBChatMessage message];

...

QBChatAttachment *attachment = QBChatAttachment.new;
attachment.type = @"image";
attachment.ID = [NSString stringWithFormat:@"%d", uploadedFileID]; //use 'ID' property to store an ID of a file in Content or CustomObjects modules

   [message setAttachments:@[attachment]];
  } statusBlock:^(QBRequest *request, QBRequestStatus *status) {
// handle progress            
 } errorBlock:^(QBResponse *response) {
NSLog(@"error: %@", response.error);
 }];

Receive attachment

For example we use Content module to store attachments. Next snippets allow to receive a message with an attachment and download it:

#pragma mark QBChatDelegate

  - (void)chatDidReceiveMessage:(QBChatMessage *)message{
for(QBChatAttachment *attachment in message.attachments){
    // download file by ID
    [QBRequest TDownloadFileWithBlobID:[attachment.ID integerValue] successBlock:^(QBResponse *response, NSData *fileData) {
        UIImage *image = [UIImage imageWithData:fileData];

    } statusBlock:^(QBRequest *request, QBRequestStatus *status) {
        // handle progress            
    } errorBlock:^(QBResponse *response) {
        NSLog(@"error: %@", response.error);
    }];
}
}

to obtain a link to attachment and use to show an image:

        - (void)chatDidReceiveMessage:(QBChatMessage *)message{
for(QBChatAttachment *attachment in message.attachments){
    // or if you have only file ID
    NSString *privateUrl = [QBCBlob privateUrlForID:[attachment.ID integerValue]];
}
}

希望对你有所帮助