简体   繁体   中英

xmpp for ios,about send message

/* 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];
- (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM