9

I am creating a chat application using XMPP Framework in iphone. i could get received messages but i am not able to send a message. can any one give me solution for this??

Yogendra
  • 1,728
  • 16
  • 28

4 Answers4

11
- (void)sendMessage:(NSString *)msgContent
{

    NSString *messageStr = textField.text;

    if([messageStr length] > 0)
    {
        NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
        [body setStringValue:messageStr];

        NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
        [message addAttributeWithName:@"type" stringValue:@"chat"];
        [message addAttributeWithName:@"to" stringValue:[jid full]];
        [message addChild:body];

        [xmppStream sendElement:message];



    }
}

use the above code in you chatViewcontroller ..it is working fine for me.

Raj
  • 5,895
  • 4
  • 27
  • 48
  • does your application gets dis-connected - as when I pass the value to send element the state is dis connected for me - do you have any idea how to resolve it ? – V.V Jul 22 '11 at 05:57
  • @iphone Fun: your question is not clear for me ..any way you can add - (void)xmppStreamDidDisconnect:(XMPPStream *)sender in your class , this will inform you when your chat disconnected from network. – Raj Jul 26 '11 at 10:58
  • Superb..! Worked for me – Nirav Dangi Oct 08 '14 at 16:00
3

Try this :

XMPPUserCoreDataStorage *user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:strSendMsg];

NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:[user.jid full]];
[message addChild:body];

[[self xmppStream] sendElement:message];
Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37
2

if you are using the xmpp iPhone example application... you can use something like the following and it should get you started:

NSString *msgText = @"test reply";

XMPPMessage* msg = [[XMPPMessage alloc] initWithType:@"chat" to:[XMPPJID jidWithString:displayName]];
[msg addBody:msgText];

[_xmppStream sendElement:msg];

just place this right below the alert they have in there in the xmppStream delegate method in

iPhoneXMPPAppDelegate.m:

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

This will automatically send "test reply" back to the jid that initially sent you the message

glhf!

greenhouse
  • 1,231
  • 14
  • 19
0

Swift 3 answer:

let user = XMPPJID(string: "user@example.com")
let msg = XMPPMessage(type: "chat", to: user)
msg?.addBody("test message")
self.xmppStream.send(msg)
Floris M
  • 1,764
  • 19
  • 18