Question

I am developing an xmpp client for iphone. i am able to send and receive message. I want to accept the buddy request automatically without prompting to user. In which function will i receive request. Please give me any hints.

Thanks in advance.

Was it helpful?

Solution

Alright, I have got a perfect answer for you!

Here is the code:

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {

    // a buddy went offline/online

    NSString *presenceType = [presence type];            // online/offline
    NSString *myUsername = [[sender myJID] user];
    NSString *presenceFromUser = [[presence from] user];

    if (![presenceFromUser isEqualToString:myUsername]) {

        if ([presenceType isEqualToString:@"available"]) {

            [_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, kHostName]];
               NSLog(@"presence user is %@",presenceFromUser);

        } 

        else if  ([presenceType isEqualToString:@"unavailable"]) {

            [_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, kHostName]];
            NSLog(@"presence user is invisible %@",presenceFromUser);

        }
        else if  ([presenceType isEqualToString:@"subscribe"]) {

            [_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, kHostName]];
                        NSLog(@"presence user wants to subscribe %@",presenceFromUser);

        }

    }
}

I have provided you with the complete code of DidReceivePresence method for your better understanding.

Now let me explain you with the code. If you notice in the else if condition I'm comparing the value (element) which we are receiving. So when I get the string as Subscribe (when the user sends the friend request), you just need to add that particular user in your tableview buddy list.

You can get the username from "presenceFromUser".

If you need anymore understanding then email me on blueobaid@gmail.com because I don't get alerts when you response to my answer, though I'll come back and reply here itself so that it might help others too! and I'm going to put a tutorial soon on http://Czartechnogeeks.com/iSolutions

I'm glad that I'm able to share something valuable to the user with stackoverflow after it helped me much.

OTHER TIPS

U won't receive request in separate function. Whenever a buddy comes to online or send request your didReceivePresence delegate function is called. if you get presence type= subscription then it is buddyrequest. Hope this helps you.

You will receive the subscription in function didReceivePresence.

To accept the subscription, you can use this code:

  NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
  [presence addAttributeWithName:@"type" stringValue:@"subscribed"];
  [presence addAttributeWithName:@"to" stringValue:[presence fromStr]];
  [presence addAttributeWithName:@"from" stringValue:@"you@host"];
  [[self xmppStream] sendElement:presence];

May it helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top