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