How we can upload image or photos to ejabberd XMPP web server?

1.3k views Asked by At

Here i am using iOS XMPP library (https://github.com/processone/demo-xmpp-ios) for my chat application, can any one help me out for uploading image from this library.thanks

1

There are 1 answers

3
James Andrews On

I would recommend uploading images to a third party server and then sending the URL via XMPP.

Here's an example using Backendless which offer 20GB of free file uploads. You would have to also follow the Backendless docs to install their SDK and setup the API keys:

[backendless.fileService.permissions grantForAllRoles:fileName operation:FILE_READ];
[backendless.fileService saveFile:fileName content:file response:^(BackendlessFile * file) {

    // We have the url so we can send the message

} error:^(Fault * fault) {
}];

Or to a PHP server using AFNetworking:

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:_url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:file name:@"upfile" fileName:name mimeType:mimeType];
} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
              uploadTaskWithStreamedRequest:request
              progress:^(NSProgress * _Nonnull uploadProgress) {
                  // This is not called back on the main queue.
                  // You are responsible for dispatching to the main queue for UI updates
                  dispatch_async(dispatch_get_main_queue(), ^{
                      //Update the progress view                           
                      [progressView setProgress:uploadProgress.fractionCompleted];
                  });
              }
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                  if (error) {
                  } else {
                      NSString *filePath = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

                      // Send message here

                  }
              }];

[uploadTask resume];

Then you can add the image and thumbnail URLs and the image dimensions to the XMPP message:

    [xmppMessage addBody:imageURL];
    [xmppMessage addAttributeWithName:bMessageImageURL stringValue:imageURL];
    [xmppMessage addAttributeWithName:bMessageThumbnailURL stringValue:thumbnailURL];
    [xmppMessage addAttributeWithName:bMessageImageWidth floatValue:width.floatValue];
    [xmppMessage addAttributeWithName:bMessageImageHeight floatValue:height.floatValue];

I'd recommend setting the body to the imageURL so that the image could be opened from a standard XMPP client.

When the message is received, you can access the details again as follows:

    NSString * imageURL = [[message attributeForName:bMessageImageURL] stringValue];
    NSString * thumbnailURL = [[message attributeForName:bMessageThumbnailURL] stringValue];
    float width = [[[message attributeForName:bMessageImageWidth] stringValue] floatValue];
    float height = [[[message attributeForName:bMessageImageHeight] stringValue] floatValue];

You could then display the image using SDWebImage.

I'd also recommend this ChatSDK which is a full front end messaging interface for iOS. It contains UITableView cells to display image messages. I used it to build an XMPP messenger using XMPPFramework.