在我的应用中,我实现了创建XMPPRoom并邀请用户。现在,我正在寻找一种方法来存储这些组(我创建的组或我被邀请的组),以便我可以在我想要的时候轻松地将其恢复到我的应用程序中。我遇到了书签XEP-0048但是,我找不到任何在线使用这个的例子。以前有人用过吗?你能分享一些例子吗?
艾哈迈德
答案 0 :(得分:3)
根据XEP-0048: Bookmarks,要将书签上传到服务器,您必须发送iq
这样的请求:
<iq from='juliet@capulet.lit/balcony' type='set' id='pip1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<publish node='storage:bookmarks'>
<item id='current'>
<storage xmlns='storage:bookmarks'>
<conference name='The Play's the Thing'
autojoin='true'
jid='theplay@conference.shakespeare.lit'>
<nick>JC</nick>
</conference>
</storage>
</item>
</publish>
<publish-options>
<x xmlns='jabber:x:data' type='submit'>
<field var='FORM_TYPE' type='hidden'>
<value>http://jabber.org/protocol/pubsub#publish-options</value>
</field>
<field var='pubsub#persist_items'>
<value>true</value>
</field>
<field var='pubsub#access_model'>
<value>whitelist</value>
</field>
</x>
</publish-options>
</pubsub>
</iq>
在使用NSXMLElement类的Objective-C中,上面的XML可以写成:
NSXMLElement *pubsub = [[NSXMLElement alloc] initWithName:@"pubsub" xmlns:@"http://jabber.org/protocol/pubsub"];
NSXMLElement *publish = [[NSXMLElement alloc] initWithName:@"publish"];
[publish addAttributeWithName:@"node" stringValue:@"storage:bookmarks"];
NSXMLElement *item = [[NSXMLElement alloc] initWithName:@"item"];
[item addAttributeWithName:@"id" stringValue:@"current"];
NSXMLElement *storage = [[NSXMLElement alloc] initWithName:@"storage" xmlns:@"storage:bookmarks"];
NSXMLElement *conference = [[NSXMLElement alloc] initWithName:@"conference"];
[conference addAttributeWithName:@"name" stringValue:@"The Play's the Thing"];
[conference addAttributeWithName:@"autojoin" stringValue:@"true"];
[conference addAttributeWithName:@"jid" stringValue:@"theplay@conference.shakespeare.lit"];
NSXMLElement *nick = [[NSXMLElement alloc] initWithName:@"nick" stringValue:@"JC"];
[conference addChild:nick];
[storage addChild:conference];
[item addChild:storage];
[publish addChild:item];
NSXMLElement *publish_options = [[NSXMLElement alloc] initWithName:@"publish-options"];
NSXMLElement *x = [[NSXMLElement alloc] initWithName:@"x" xmlns:@"jabber:x:data"];
[x addAttributeWithName:@"type" stringValue:@"submit"];
NSXMLElement *field1 = [[NSXMLElement alloc] initWithName:@"field"];
[field1 addAttributeWithName:@"var" stringValue:@"FORM_TYPE"];
[field1 addAttributeWithName:@"type" stringValue:@"hidden"];
NSXMLElement *value1 = [[NSXMLElement alloc] initWithName:@"value" stringValue:@"http://jabber.org/protocol/pubsub#publish-options"];
[field1 addChild:value1];
[x addChild:field1];
NSXMLElement *field2 = [[NSXMLElement alloc] initWithName:@"field"];
[field2 addAttributeWithName:@"var" stringValue:@"pubsub#persist_items"];
NSXMLElement *value2 = [[NSXMLElement alloc] initWithName:@"value" stringValue:@"whitelist"];
[field2 addChild:value2];
[x addChild:field2];
[publish_options addChild:x];
[pubsub addChild:publish];
[pubsub addChild:publish_options];
XMPPIQ *iq = [[XMPPIQ alloc] initWithType:@"set" child:pubsub];
[iq addAttributeWithName:@"from" stringValue:@"juliet@capulet.lit/balcony"];
[iq addAttributeWithName:@"id" stringValue:@"pip1"];
当然它只能用一个NSXMLElement编写,比如
NSXMLElement *iq = [NSXMLElement alloc] initWithName:@"iq" stringValue:[NSString stringWithFormat:@"all xml code from first paragraph with %@ to add your dynamic data...", data1, data2, ...];
[iq addAttributeWithName:@"from" stringValue:@"juliet@capulet.lit/balcony"];
[iq addAttributeWithName:@"id" stringValue:@"pip1"];
创建iq后,是时候将其发送到服务器,如
[xmppStream sendElement:iq];
这就是书签发送到服务器的方式。您在XMPPStream委托- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
中侦听服务器响应,服务器响应应该是这样的:
<iq to='juliet@capulet.lit/balcony' type='result' id='pip1'/>
或在objective-c代码中:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
if([iq isResultIQ])
{
if([[iq attributeStringValueForName:@"id"] isEqualToString:@"pip1"])
{
NSLog(@"Bookmarks with id %@ succesfully uploaded", [iq attributeStringValueForName:@"id"]);
}
}
}
现在,基于上面的示例,为xml创建Objective-c代码,clinet从服务器请求书签(XEP-0048: Bookmarks 3.3 Retrieving Data):
<iq from='juliet@capulet.lit/randomID' type='get' id='retrieve1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<items node='storage:bookmarks'/>
</pubsub>
</iq>
objective-c代码:
NSXMLElement *pubsub = [[NSXMLElement alloc] initWithName:@"pubsub" xmlns:@"http://jabber.org/protocol/pubsub"];
NSXMLElement *items = [[NSXMLElement alloc] initWithName:@"items"];
[items addAttributeWithName:@"node" stringValue:@"storage:bookmarks"];
[pubsub addChild:items];
XMPPIQ *iq = [[XMPPIQ alloc] initWithType:@"get" child:pubsub];
[iq addAttributeWithName:@"from" stringValue:@"juliet@capulet.lit/balcony"];
[iq addAttributeWithName:@"id" stringValue:@"retrive1"];
像以前一样将它发送到服务器:
[xmppStream sendElement:iq];
并像以前一样监听服务器响应:
<iq type='result'
to='juliet@capulet.lit/randomID'
id='retrieve1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<items node='storage:bookmarks'>
<item id='current'>
<storage xmlns='storage:bookmarks'>
<conference name='The Play's the Thing'
autojoin='true'
jid='theplay@conference.shakespeare.lit'>
<nick>JC</nick>
</conference>
</storage>
</item>
</items>
</pubsub>
</iq>
或objective-c代码:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
if([iq isResultIQ])
{
if([[iq attributeStringValueForName:@"id"] isEqualToString:@"retrive1"])
{
NSXMLElement *pubsub = [iq elementForName:@"pubsub"];
NSArray *items = [pubsub elementsForName:@"items"];
NSLog(@"Bookmarks for id %@ are: %@", [iq attributeStringValueForName:@"id"], items);
}
}
}
答案 1 :(得分:1)
每当您创建一个组或接受聊天室邀请时,请使用必要的信息将该组存储到本地数据库。退出组时从数据库中删除它。
答案 2 :(得分:1)
从jabber服务器获取用户组。
XMPPIQ *iq = [[XMPPIQ alloc]init];
[iq addAttributeWithName:@"type" stringValue:@"get"];
NSString *from = [NSString stringWithFormat:@"%@/ResouseName",[[AppDelegate instance] xmppStream].myJID.bare];
[iq addAttributeWithName:@"from" stringValue:from];
NSXMLElement *query =[NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:private"];
NSXMLElement *storage = [NSXMLElement elementWithName:@"storage" xmlns:@"storage:bookmarks"];
[query addChild:storage];
[iq addChild:query];
[xmppStream sendElement:iq];
答案 3 :(得分:0)
你很容易这样做。 请试试这个。 您可以使用此方法添加到服务器中的书签。
-(void)bookMark :(NSString *)roomName{
XMPPIQ *iq = [[XMPPIQ alloc]init];
[iq addAttributeWithName:@"type" stringValue:@"set"];
NSString *from = [NSString stringWithFormat:@"%@/ResouseName",[[AppDelegate instance] xmppStream].myJID.bare];
[iq addAttributeWithName:@"from" stringValue: from];
NSXMLElement *query =[NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:private"];
NSXMLElement *storage = [NSXMLElement elementWithName:@"storage" xmlns:@"storage:bookmarks"];
NSXMLElement *conference_s = [NSXMLElement elementWithName:@"conference"];
[conference_s addAttributeWithName:@"autojoin" stringValue:@"true"];
[conference_s addAttributeWithName:@"jid" stringValue:roomName];
[storage addChild:conference_s];
[query addChild:storage];
[iq addChild:query];
NSLog(@"print eml log %@:",iq);
[xmppStream sendElement:iq];
}
答案 4 :(得分:0)
最后,您可以获得您的小组列表。
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
NSXMLElement *pubsub = [iq elementForName:@"query"];
NSXMLElement *storage = [pubsub elementForName:@"storage"];
NSArray *conferenceName = [storage elementsForName:@"conference"];
for (NSXMLElement *conference in conferenceName) {
NSString *jid = [[conference attributeForName:@"jid"]stringValue];
NSLog(@"print conference jid=====%@",jid);
}
NSLog(@"Bookmarks for id are: %@", conferenceName);