Arvinder Singh
Arvinder Singh

Reputation: 109

Send attachment to QuickBlox IOS sdk

I have downloaded the quickblox sample chat application and sending text messages are working fine. But how to send attachment like picture, video or else?

According to Quickblox's documentation. There is class named QBChatAttachment having properties as type, url and id but how to attach files like picture, video or else?

Upvotes: 1

Views: 1704

Answers (1)

Badal Shah
Badal Shah

Reputation: 7602

Please Proper read SimpleSample-chat users-ios in this link they have mention all detail regarding how to send attachment and how to receive and download attachment etc .

For sending and receiving attachment in quickbox follow this link Send and Receive Attachment

Detail Explanation:

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]];
}
}

I hope it will be helpful for you.

Upvotes: 4

Related Questions