0

/* how to use xmpp protocol to send message to a specified person in an IOS app,here is my code,but it can't work,i guess my parameters in method are wrong,i don't konw what parameters should be written in method,please help me */

NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:textfd.text];
NSXMLElement *message1 = [NSXMLElement elementWithName:@"message"];
[message1 addAttributeWithName:@"type" stringValue:@"chat"];
[message1 addAttributeWithName:@"to" stringValue:@"admin"];
[message1 addChild:body];
_xmppStream = [[XMPPStream alloc]init];
[_xmppStream sendElement:message1];
3
  • You need to connect and authenticate your xmpp stream first before you can send any message.
    – Simon
    Jul 23, 2014 at 7:21
  • - (void)sendMessage{ _xmppStream = [[XMPPStream alloc] init]; [_xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; [body setStringValue:textfd.text]; NSXMLElement *message = [NSXMLElement elementWithName:@"message"]; [message addAttributeWithName:@"type" stringValue:@"chat"]; NSString *user = @"admin"; NSString *to = [NSString stringWithFormat:@"%@@tp_web", user]; [message addAttributeWithName:@"to" stringValue:to]; [message addChild:body]; [self.xmppStream sendElement:message]; } Jul 23, 2014 at 7:46
  • 1
    You are in lack of basic knowledge about XMPP. You can download XMPPframework from github, and check the example in XMPPFramework/Xcode/iPhoneXMPP. You will get what you want about "XMPP connect and authenticate". After that, you can send your chat message.
    – Simon
    Jul 23, 2014 at 8:16

1 Answer 1

0
- (void)sendMessage:(Message *)message
{
    if(message)
    {
        NSXMLElement * msg =  [self composeMessage:message];
        [_xmppStream sendElement:msg];
    }
    else
    {
        NSLog(@"message arguments missing");
    }
}

- (NSXMLElement *)composeMessage:(Message *)msg
{
    NSXMLElement *returnValue;
    if(msg.type == IMChat)
    {
        NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
        [body setStringValue:msg.text];
        NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
        [message addAttributeWithName:@"type" stringValue:@"chat"];// IMChat
        [message addAttributeWithName:@"to" stringValue:msg.recipientId];
        [message addChild:body];
        returnValue = message;
    }
    else
    {
        NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
        [body setStringValue:msg.text] ;
        NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
        [message addAttributeWithName:@"type" stringValue:@"groupchat"];//Group Chat
        [message addAttributeWithName:@"to" stringValue:msg.recipientId];
        [message addChild:body];
        returnValue = message;
    }

    return returnValue;

}

Message model contain..

typedef enum MessageTypes
{
    IMChat = 1,
    GroupChat = 2
} MessageTypes;

@interface Message : NSObject

@property (nonatomic, retain) NSString *text;
@property (nonatomic, assign) MessageTypes type;
@property (nonatomic, retain) NSString *senderId;
@property (nonatomic, retain) NSString *recipientId;

@end
4
  • Before u have to initialise xmmppStream and set delegates.FYI:recipientId or senderId, it should be full id like admin@domainName Jul 23, 2014 at 7:18
  • let me know the status. Jul 23, 2014 at 7:23
  • - (void)sendMessage{ _xmppStream = [[XMPPStream alloc] init]; [_xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; [body setStringValue:textfd.text]; NSXMLElement *message = [NSXMLElement elementWithName:@"message"]; [message addAttributeWithName:@"type" stringValue:@"chat"]; NSString *user = @"admin"; NSString *to = [NSString stringWithFormat:@"%@@tp_web", user]; [message addAttributeWithName:@"to" stringValue:to]; [message addChild:body]; [self.xmppStream sendElement:message]; } Jul 23, 2014 at 7:48
  • its worked for me.checking ur IDs.Try configure account with iChat or Adium, to make sure Ur IDs are proper Jul 23, 2014 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.