gpt4 book ai didi

ios - XMPPFramework - XEP-0048 : Bookmark Storage

转载 作者:行者123 更新时间:2023-11-28 18:35:02 27 4
gpt4 key购买 nike

在我的应用中,我实现了创建 XMPPRoom 和邀请用户。现在,我正在寻找一种方法来存储这些组(我创建的组或我受邀加入的组),以便我可以在需要时轻松地将其检索回我的应用程序。我遇到了 XEP-0048 书签,但是,我找不到任何在线使用它的示例。有人用过这个吗?能否分享一些示例?

http://www.xmpp.org/extensions/attic/xep-0048-1.0.html

艾哈迈德

最佳答案

根据 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&apos;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&apos;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 委托(delegate) - (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"]);
}
}
}

现在,基于上面的示例,为 clinet 从服务器(XEP-0048: Bookmarks 3.3 Retrieving Data)请求书签的 xml 创建 objective-c 代码:

<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&apos;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);
}
}
}

关于ios - XMPPFramework - XEP-0048 : Bookmark Storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com