XMPPFramework - XEP-0048:书签存储 [英] XMPPFramework - XEP-0048: Bookmark Storage
问题描述
在我的应用中,我实现了创建 XMPProom 和邀请用户.现在,我正在寻找一种方法来存储这些组(我创建的组或我受邀加入的组),以便我可以在需要时轻松地将其检索回我的应用程序.我遇到了书签 XEP-0048,但是,我在网上找不到任何使用它的示例.有没有人以前用过这个?你能分享一些例子吗?
In my app, I have implemented creating XMPPRoom and inviting users. Now, I am searching for a way to store these groups (my created or the groups i have been invited to) so that i can easily retrieve it back in my app when I want. I came across bookmark XEP-0048 however, I can't find any example of using this online. Has anyone used this before? Can you please share some examples?
http://www.xmpp.org/extensions/attic/xep-0048-1.0.html
艾哈迈德
推荐答案
根据 XEP-0048:书签,要将书签上传到服务器,您必须像这样发送 iq
请求:
According to XEP-0048: Bookmarks, to upload bookmarks to server, you have to send a iq
request like this:
<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 可以这样写:
In Objective-C using NSXMLElement class, above XML can be written as this:
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 来写,比如
Of course it could be written with only one NSXMLElement, something like
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 之后,是时候将它发送到服务器了,就像
After iq has been created it is time to send it to the server like
[xmppStream sendElement:iq];
这是将书签发送到服务器的方式.您在 XMPPStream 委托 - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
中监听服务器响应,服务器响应应该是这样的:
and this is the way bookmarks are sent to server. You listen for server response in in XMPPStream delegate - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
and server response should be something like this:
<iq to='juliet@capulet.lit/balcony' type='result' id='pip1'/>
或在objective-c代码中:
or in objective-c code:
- (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:书签 3.3 检索数据):
Now, based on example from above create objective-c code for xml with which clinet is requesting bookmarks from server (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 代码:
objective-c code:
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"];
像以前一样发送到服务器:
send it to the server like before:
[xmppStream sendElement:iq];
并像以前一样监听服务器响应:
and listen for server response like before:
<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代码:
or objective-c code:
- (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);
}
}
}
这篇关于XMPPFramework - XEP-0048:书签存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!