XMPPMessage time - ios

I'am working with XMPPFramework for iOS and I need to show message's date and time.
After some searching I think that I should use XMPPAutoTime module, but I didn't find some examples.
I used in stream setup:
xmppAutoTime = [[XMPPAutoTime alloc] initWithDispatchQueue:dispatch_get_main_queue()];
[xmppAutoTime activate:self.xmppStream];
My sending message method looks like:
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:to];
[message addChild:[XMPPTime timeElement]];
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:text];
[message addChild:body];
And my XML looks like:
<message type="chat" to="toJID">
<time xmlns="urn:xmpp:time">
<tzo>+03:00</tzo>
<utc>2014-12-30T15:24:22Z</utc>
</time>
<body>message text</body>
</message>
So as you can see I have time in place and it is correct. The question is how I can use it for XMPPMessage
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message

I'm working on XMPP too and I'm glad you had this question, as I didn't know how to add time to the messages I send.
Anyway, I'm not sure if there's an easier or a more correct way of doing it, but I use this:
[[message elementForName:#"time"] elementForName:#"utc"]
inside the didReceiveMessage delegate method to extract the time from the XML.

Related

Send Image by xmppfrmaework

I have set up ejabberd server on localhost, then I registered an account on Adium by selecting XMPP (jabber) server on port 5222. After that I configured an account on mobile test app and sent messages between two users (Adium/test app) successfully. But I am facing an issue(no error messages in console) while sending image from my test app to Adium. It is not received on Adium.
NSData *dataF = UIImageJPEGRepresentation(imageName, 0.0);
NSString *imgStr = [dataF base64EncodedStringWithOptions: 0];
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:#"123"];
NSXMLElement *ImgAttachement = [NSXMLElement elementWithName:#"attachment"];
[ImgAttachement setStringValue: imgStr];
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:jid];
[message addChild:body];
[message addChild:ImgAttachement];
[self.xmppStream sendElement:message];
Any help would be much appreciated.
We saw a similar issue in our ios app. I think you need to somehow add the two users as friends/contacts and make sure you have enabled mod_vcard_xupdate module in ejabberd config file. See if that helps.

Proper way to uniquely identify a message sent via XMPP in delegate method?

I am writing an iOS chat application using XMPPFramework, and I am wondering what's the best way to create a new message so I can map the XMPPMessage that gets returned in one of the delegate methods back to the original message that is sent. I want to do this so I can tell when sending a message is successful or unsuccessful (so I can cache and resend it).
Upon the user hitting the send key on the client, the following code is called to create and send the message:
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:messageText];
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:recipientJID];
[message addChild:body];
[[self xmppStream] sendElement:message];
Then I store the contents, recipient, timestamp, etc. into a custom object (basically everything in message) in an array.
I want to be able to match an object in the array to the XMPPMessage object returned in either of these delegate methods:
- (void)xmppStream:(XMPPStream *)sender didFailToSendMessage:(XMPPMessage *)message error:(NSError *)error
{
}
- (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message
{
}
Is there an attribute that I can add some kind of unique identifier to in the message variable that I send via XMPPStream that will then show up within the XMPPMessage passed to those delegate methods?
Why don't you send a XMPPMessage instead of a NSXMLElement? That way the object passed in didFailToSendMessage and didSendMessage will match the one you send. Besides, you can set elementId to identify stanzas (Messages, IQs or Presence)
XMPPMessage *message = [XMPPMessage messageWithType:#"chat"
to:someJid
elementID:elementId];
[[self xmppStream] sendElement:message];

XMPPFramework - 'didReceiveMessage' called two times when message is received

Whenever I receive any new messages, these two methods gets called two times.
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
I am sending request to server to get my roster list with the following code.
NSXMLElement *query = [NSXMLElement elementWithName:#"query" xmlns:#"jabber:iq:roster"];
XMPPIQ *iq = [XMPPIQ iq];
[iq addAttributeWithName:#"id" stringValue:#"buddyRequest"];
[iq addAttributeWithName:#"to" stringValue:#"54.186.107.171"];
[iq addAttributeWithName:#"type" stringValue:#"get"];
[iq addChild:query];
[self.xmppStream sendElement:iq];
Could somebody point out to me where the problem might lie?
Thank you.
I think you set the delegate to XMPPStream twice.Remove one and check.Then also u got two response , check the xmppstream object value.is both are different , surely you set twice.

XMPPFramework - How to transfer image from one device to another?

I have made one-to-one chat using XMPP protocol. Now, I would like to send image and video in my application. I researched about the file transfer but I didn't find a solution. I have also used the code below for Socket connection.
Please advice me on how I can go about doing this.
[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 connection fail method call..
Thanks.
you need push the image to server and you will reveice a url from server .then you can send the url to another device by xmpp protocol. in the end. download the image from server by the received url.
xmpp also can send image . But that's a big xml message for xmpp server .that's not a great solution.
Try this...
NSData *dataF = UIImagePNGRepresentation(SendImage);
NSString *imgStr=[dataF base64Encoding];
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:messageStr];
NSXMLElement *imgAttachement = [NSXMLElement elementWithName:#"attachment"];
[imgAttachement setStringValue:imgStr];
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:chatWithUser];
[message addChild:body];
[message addChild:imgAttachement];
[self.xmppStream sendElement:message];
I hope this will help you...

Message not sent through XMPP

I am using XMPP in my app. My messages are not being sent whenever I attempt to send a message to any specific ID (for example 'test.codemen#gmail.com' in my code).
My code is given below. Thanks in advance.
- (IBAction)sendMsg:(id)sender
{
NSXMLElement *body = [NSXMLElement elementWithName:#"body"];
[body setStringValue:msgField.text];
NSXMLElement *message = [NSXMLElement elementWithName:#"message"];
[message addAttributeWithName:#"type" stringValue:#"chat"];
[message addAttributeWithName:#"to" stringValue:#"test.codemen#gmail.com"];
[message addChild:body];
iPhoneXMPPAppDelegate *share = [iPhoneXMPPAppDelegate sharedInstance];
[[share xmppStream] sendElement:message];
}
your code looks fine to me.
Google Talk routes messages only to contacts you are subscribed to. Is the user you send the message to on your contact list?
Print the xml , then see what's in the message .Maybe you are sending message to a wrong JID .

Resources