How to transfer image One device to another device using xmpp framework on iphone?

I made a one-to-one chat application using the xmpp protocol. Now I want to send image and video to my application. I have a lot of information about file transfer but I haven't found a solution. I am using below code to connect Socket.

Please help me how can I do this.

My code:

[TURNSocket setProxyCandidates:@[@"MyserverHost-desktop"]]; 

XMPPJID *jid = [XMPPJID jidWithString:@"1254225445@MyserverHost-desktop"];

 TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:[[self appDelegate]xmppStream] toJID:jid]; 

[app.turnSocketArray addObject:turnSocket]; 

[turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()]; 
[turnSocket release]; 

- (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket 
{ 

}
- (void)turnSocketDidFail:(TURNSocket *)sender
{

}

      

Every time the connection refused method is called.

Thank.

+3


source to share


2 answers


Even I don't post the image directly to xmpp because it takes a long time.

In my project I post can be a string and it can be an image and I found that there is only one method to send ie post.

[xmppRoom sendMessageWithBody:@"Your msg"];

      

So, to send an image, I first convert it to base64 and then upload it to my server and get the URL for that image from the server. Once we get the url, just pass that url in the method above.

The problem I ran into was how to separate normal posts and urls (images)

So, for sending plain text I send it directly to above function, but for sending url I send nsdictionary ie I convert nsdictionary to string and send it to above function



NSDictionary *dict = @{@"type":@"image",
                               @"url":@"Url of your image"};

            NSString *newMessage = [NSString stringWithFormat:@"%@",dict];

            [appDelegate.xmppRoom sendMessageWithBody:newMessage];

      

To separate the normal message and the picture in

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{

    messages *msg = [messages new];

     NSDictionary *dictOfMedia = [NSPropertyListSerialization
                                 propertyListWithData:[[[message elementForName:@"body"] stringValue] dataUsingEncoding:NSUTF8StringEncoding]
                                 options:kNilOptions
                                 format:NULL
                                 error:NULL];

    if ([dictOfMedia objectForKey:@"type"])
    {
        msg.mediaType = [dictOfMedia objectForKey:@"type"];
        msg.url = [dictOfMedia objectForKey:@"url"];
    }
    else
    {
        msg.msg = [[message elementForName:@"body"] stringValue];
    }


}

      

posts is my model class you can use just to use nsstring.

After that, just use any open source project to load the image or perform lazy loading yourself.

Hope this helps you :)

0


source


I am using a different method to transfer the media file.

1) The sender uploads the media file to the server and receives the url.

2) Senders send JSON like this: {"type": "Image", "URL": "this is the URL for the image"}



3) Receiver receives JSON and downloads image from server.

4) The recipient shows the picture.

0


source







All Articles