1

I am successfully able to receive presence i.e. status of users in my iOS XMPP client. But within that status is some extra info that I wish to extract. This info was added as a child to the presence while sending.

Here is how I extended presence while sending:

- (void)updatePresence:(NSNotification *)notification
{
    XMPPPresence *presence = [XMPPPresence presence];
    NSString *string = [notification object]; // object contains some random string.
    NSXMLElement *status = [NSXMLElement elementWithName:@"status" stringValue:string];
    [presence addChild:status];
    NSLog(@"presence info :- %@",presence);
    [[self xmppStream] sendElement:presence];
}

Now when I am receiving presence I want to retrieve this extended portion of the presence. How can it be done?

This is how I receive the presence:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    NSString *presenceType = [presence type]; // online / offline
    NSString *myUsername = [[sender myJID] user];
    NSString *presenceFromUser = [[presence from] user];
    NSString *presenceString=[presence fromStr];
    NSString *string = @"@company.com";

    if ([presenceString rangeOfString:string].location == NSNotFound)
    {
        if (![presenceFromUser isEqualToString:myUsername])
        {
            if ([presenceType isEqualToString:@"available"])
            {
                NSMutableDictionary *buddy=[[NSMutableDictionary alloc]init];
                [buddy setObject:presenceFromUser forKey:@"name"];
                [buddy setObject:[presence fromStr] forKey:@"jid"];

                [_chatDelegate newBuddyOnline:buddy];
            }
            else if ([presenceType isEqualToString:@"unavailable"])
            {
                NSMutableDictionary *buddy=[[NSMutableDictionary alloc]init];
                [buddy setObject:presenceFromUser forKey:@"name"];
                [buddy setObject:[presence fromStr] forKey:@"jid"];
                [_chatDelegate buddyWentOffline:buddy];
            }
        }
    }
}
Keith OYS
  • 2,285
  • 5
  • 32
  • 38
iCodes
  • 1,382
  • 3
  • 21
  • 47

1 Answers1

4

Presence Packet look like this

    //    <presence xmlns="jabber:client" 
    //    id="Jothb-6" 
    //    from="sender@domain.com/resource" 
    //    to="receiver@domain.com">
    //    <status>Online</status>
    //    <show>presence message</show>
    //    <priority>1</priority>
    //    </presence>

To get this:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
        NSXMLElement *showStatus = [presence elementForName:@"status"];
        NSString *presenceString = [showStatus stringValue];
        NSString *customMessage = [[presence elementForName:@"show"]stringValue];

        NSLog(@"Presence : %@, and presenceMessage: %@",presenceString,customMessage);
}
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29