Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

XMPPFramework - XEP-0048: Bookmark Storage

XMPPFramework - XEP-0048: Bookmark Storage


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

Answer by Sihad Begovic for XMPPFramework - XEP-0048: Bookmark Storage


According to XEP-0048: Bookmarks, to upload bookmarks to server, you have to send a iq request like this:

                                                      JC                                                                        http://jabber.org/protocol/pubsub#publish-options                                true                                whitelist                                

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:

  

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):

                  

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:

                                                      JC                                            

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

Answer by PETER for XMPPFramework - XEP-0048: Bookmark Storage


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.

Answer by TMD for XMPPFramework - XEP-0048: Bookmark Storage


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

Answer by TMD for XMPPFramework - XEP-0048: Bookmark Storage


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];  

Answer by TMD for XMPPFramework - XEP-0048: Bookmark Storage


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.