xmpp for ios,关于发送消息

时间:2014-07-23 06:36:05

标签: ios

/ * 如何使用xmpp协议向IOS应用程序中的指定人员发送消息,这是我的代码,但它无法正常工作,我猜我的方法参数是错误的,我不知道应该在方法中写什么参数,请帮我 * /

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];

1 个答案:

答案 0 :(得分: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;

}

消息模型包含..

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