问题
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
Ahmad
回答1:
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>
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"];
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"];
After iq has been created it is time to send it to the server like
[xmppStream sendElement: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'/>
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"]);
}
}
}
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 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>
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);
}
}
}
回答2:
Whenever you create a group or accepts a chatroom invitation, store the group to local database with the necessary information. Delete it from db while exiting the group.
回答3:
Get User groups from jabber server.
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];
回答4:
You very easy to doing this way. Please try this. you can add to bookmark in server using this method.
-(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];
}
回答5:
Finally you can get your group list.
- (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);
来源:https://stackoverflow.com/questions/20792739/xmppframework-xep-0048-bookmark-storage