1

我成功地能够在我的 iOS XMPP 客户端中接收用户的状态,即状态。但在那个状态中是我希望提取的一些额外信息。此信息在发送时作为子项添加到状态中。

这是我在发送时扩展存在的方式:

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

现在,当我收到出席信息时,我想检索出席信息的这个扩展部分。怎么做到呢?

这就是我收到出席信息的方式:

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

1 回答 1

4

存在数据包看起来像这样

    //    <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>

要得到这个:

- (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);
}
于 2013-11-22T12:49:26.143 回答