在iphone中使用XMPP创建Custom Stanza

时间:2014-09-26 06:46:49

标签: ios objective-c iphone xmpp xmppframework

我尝试使用以下代码创建自定义数据包但是当它转到XMPP lifeCycle时它会被更改。

NSString * strUser = [[NSUserDefaults standardUserDefaults] valueForKey:@"kXMPPmyJID"];

XMLWriter* xmlWriter = [[XMLWriter alloc]init];

[xmlWriter writeStartElement:@"presence"];
[xmlWriter writeAttribute:@"from" value:strUser];
[xmlWriter writeAttribute:@"to" value:appDelegate.chatUser];
[xmlWriter writeAttribute:@"type" value:@"typing.."];
[xmlWriter writeEndElement];

NSString* xml = [xmlWriter toString];

NSXMLElement *xmlElement = [[NSXMLElement alloc] initWithXMLString:xml error:nil];
XMPPPresence *presence = [XMPPPresence presenceFromElement:xmlElement];
[appDelegate.xmppStream sendElement:presence];

2 个答案:

答案 0 :(得分:1)

如果检查xmpp协议(http://xmpp.org/rfcs/rfc3921.html) 他们说type属性必须具有协议中规定的值之一。如果要添加自定义属性,则应将其添加到自定义标记中,但接收方必须正确处理它。

答案 1 :(得分:0)

您应该在您的节中添加一个自定义元素,而不是将其设置为存在的属性:

[xmlWriter writeStartElement:@"presence"];
[xmlWriter writeAttribute:@"from" value:strUser];
[xmlWriter writeAttribute:@"to" value:appDelegate.chatUser];
[xmlWriter writeStartElementWithNamespace:@"vikram" localName:@"typing"];
[xmlWriter writeEndElement];
[xmlWriter writeEndElement];

将创建:

<presence id='...' from='...' to='...'><typing xmlns='vikram'/></presence>

虽然使用在线状态发送输入通知是一个坏主意。为什么不使用XEP-0085