0

这是我用来发送好友请求的代码,工作正常

XMPPJID *newBuddy = [XMPPJID jidWithString:userName];
[[self appDelegate].xmppRoster addUser:newBuddy withNickname:nil];

这是我用来接受好友请求的代码,工作正常。

XMPPJID *newBuddy = [XMPPJID jidWithString:ipAddress];
     [[self appDelegate].xmppRoster acceptPresenceSubscriptionRequestFrom:newBuddy andAddToRoster:YES];

问题是,每当再次接受请求时,我都会从接受的用户那里获得订阅,这就是问题所在。如何在 xmpp 中停止双向订阅帮助我

4

3 回答 3

2
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    DDLogVerbose(@"%@: %@ - %@", THIS_FILE, THIS_METHOD, [presence fromStr]);

    NSString *presenceType = [presence type];            // online/offline
    NSString *myUsername = [[sender myJID] user];
    NSString *presenceFromUser = [[presence from] user];
    //
    //new request from unknow user
    if (![presenceFromUser isEqualToString:myUsername])
    {
        if  ([presenceType isEqualToString:@"subscribe"])
        {
            //[_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, kHostName]];
            NSLog(@"presence user wants to subscribe %@",presenceFromUser);
            tempPresence = [[XMPPPresence alloc] init];
            tempPresence = presence;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New request From:" message:presenceFromUser delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
            [alert show];
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //accept request
    if(buttonIndex==1){
        [xmppRoster subscribePresenceToUser:[tempPresence from]];
    }
}
于 2013-07-18T05:09:35.700 回答
1
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence {
    XMPPUserCoreDataStorageObject *user = [self.xmppRosterStorage
                                           userForJID:[presence from]
                                           xmppStream:self.xmppStream
                                           managedObjectContext:[self managedObjectContext_roster]];
    DDLogVerbose(@"didReceivePresenceSubscriptionRequest from user %@ ",
                 user.jidStr); [self.xmppRoster
                                acceptPresenceSubscriptionRequestFrom:[presence from]
                                andAddToRoster:YES];
}
于 2014-05-28T12:30:25.733 回答
0

最后,我通过使用用户名和所有用户搜索用户找到了替代答案。

1.对于每个带有 gmail id 的联系人 user@gmail.com

2.对于具有特定名称的特定用户,请使用 userName。

3.要从开火服务器获取所有用户,只需在搜索字符串中输入“*”。

//Request
- (void)getDetailsofRegisteredUser {

    //To Search Peticular User either by using their name, email or username
    NSString *userBare1  = [[[[self appDelegate] xmppStream] myJID] bare];
    NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
    [query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:search"];

    NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
    [x addAttributeWithName:@"type" stringValue:@"submit"];

    NSXMLElement *formType = [NSXMLElement elementWithName:@"field"];
    [formType addAttributeWithName:@"type" stringValue:@"hidden"];
    [formType addAttributeWithName:@"var" stringValue:@"FORM_TYPE"];
    [formType addChild:[NSXMLElement elementWithName:@"value" stringValue:@"jabber:iq:search" ]];

    NSXMLElement *userName = [NSXMLElement elementWithName:@"field"];
    [userName addAttributeWithName:@"var" stringValue:@"Username"];
    [userName addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1" ]];


- (void)getDetailsofRegisteredUser {

    //To Search Peticular User either by using their name, email or username
    NSString *userBare1  = [[[[self appDelegate] xmppStream] myJID] bare];
    NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
    [query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:search"];

    NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
    [x addAttributeWithName:@"type" stringValue:@"submit"];

    NSXMLElement *formType = [NSXMLElement elementWithName:@"field"];
    [formType addAttributeWithName:@"type" stringValue:@"hidden"];
    [formType addAttributeWithName:@"var" stringValue:@"FORM_TYPE"];
    [formType addChild:[NSXMLElement elementWithName:@"value" stringValue:@"jabber:iq:search" ]];

    NSXMLElement *userName = [NSXMLElement elementWithName:@"field"];
    [userName addAttributeWithName:@"var" stringValue:@"Username"];
    [userName addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1" ]];

    NSXMLElement *name = [NSXMLElement elementWithName:@"field"];
    [name addAttributeWithName:@"var" stringValue:@"Name"];
    [name addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];

    NSXMLElement *email = [NSXMLElement elementWithName:@"field"];
    [email addAttributeWithName:@"var" stringValue:@"Email"];
    [email addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];

    //Here in the place of SearchString we have to provide registered user name or emailid or username,For all user put @"*"  (if it matches in Server it provide registered user details otherwise Server provides response as empty)
    NSXMLElement *search = [NSXMLElement elementWithName:@"field"];
    [search addAttributeWithName:@"var" stringValue:@"search"];
    [search addChild:[NSXMLElement elementWithName:@"value" stringValue:[NSString stringWithFormat:@"%@", SearchString]]];

    [x addChild:formType];
    [x addChild:userName];
    [x addChild:name];
    [x addChild:email];
    [x addChild:search];
    [query addChild:x];

    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"id" stringValue:@"searchByUserName"];
    [iq addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"search.%@",DOMAIN_NAME]];
    [iq addAttributeWithName:@"from" stringValue:userBare1];
    [iq addChild:query];
    [[[self appDelegate] xmppStream] sendElement:iq];

}

//We will get response here

- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{

    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    NSLog(@"searched format %@", iq);

}
于 2014-09-29T10:40:38.867 回答