10

I'm able to create a MUC using XMPPFramework and send user invitation requests to join that room by using the code below.

// Creating
AppDelegate *dele =(AppDelegate *) [[UIApplication sharedApplication]delegate];

xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:dele jid:[XMPPJID jidWithString:self.roomName] dispatchQueue:dispatch_get_main_queue()];
[xmppRoom addDelegate:dele delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:dele.xmppStream];
[xmppRoom joinRoomUsingNickname:self.myNick history:nil];

// Inviting
[xmppRoom inviteUser:[XMPPJID jidWithString:@"abc@host"] withMessage:@"Come Join me"];

How does a user "abc" know that he has received an invitation and how can he react to it either by accepting or declining?

I could not find any class in XMPPFramework which directly deal with chat room invitation. My research says that whenever a user receives an chatroom invitation, xmmppStream's delegate method is called:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message

In that message, I check whether the it contains NSXMLElement with name "invite", and if it contains then i send a callback to the user. Then i create chatroom with the same name as the name of the chatroom from which user received invitation and enter that newly created room. It works fine but quiet lengthy and not quiet efficient. I want to know if there is a class in XMPPFramework available here which could handle chat room invitation separately. For instance, detecting, accepting, and declining of room invitations.

My code for extracting room name:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    NSXMLElement * x = [message elementForName:@"x" xmlns:XMPPMUCUserNamespace];
    NSXMLElement * invite  = [x elementForName:@"invite"];
    NSXMLElement * decline = [x elementForName:@"decline"];
    NSXMLElement * directInvite = [message elementForName:@"x" xmlns:@"jabber:x:conference"];
    NSString *msg = [[message elementForName:@"body"]stringValue];
    NSString *from = [[[message attributeForName:@"from"]stringValue];
    if (invite || directInvite)
    {
        [self createAndEnterRoom:from Message:msg];
        return;
    }
    [self.delegate newMessageRecieved:msg];
}
Nicholas
  • 7,403
  • 10
  • 48
  • 76
Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
  • Hi Vishal, I am stuck in similar issue, Can you please let me know Have you found any solution or approach with this ? Thanks – Mangesh Oct 03 '14 at 14:00

2 Answers2

14

For room invitations and declines, implement XMPPMUCDelegate and its methods -xmppMUC:didReceiveRoomInvitation: and -xmppMUC:didReceiveRoomInvitationDecline:.

To get the room JID, invoke [message from];

To join the room, instantiate an XMPPRoom and invoke -joinRoomUsingNickname:history:.

Then have your room delegate class implement XMPPRoomDelegate, and implement some of the delegate methods to handle receiving messages in the room.

It looks like there isn't at present a more automatic way to respond to invitations.

Update: The delegate callbacks now receive the room JID as a parameter, clarifying the semantics a bit.

- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitation:(XMPPMessage *)message;
- (void)xmppMUC:(XMPPMUC *)sender roomJID:(XMPPJID *) roomJID didReceiveInvitationDecline:(XMPPMessage *)message;
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • But I can only implement xmpproomDelegate after creating a xmpproom. xmpproom delegate methods do not provide any call back for room invitation, i gues they are just for handling incoming and outgoing message and some other stuffs of a particular room. – Vishal Singh Feb 02 '13 at 19:48
  • For that you want `XMPPMUCDelegate`. – paulmelnikow Feb 02 '13 at 19:56
  • yes but how do i accept the invitation??how do i enter that room and handle all incoming messages of that room? Do i have have to create a room with the same name and then implement its delegate methods as i mention in my question?? – Vishal Singh Feb 02 '13 at 20:12
  • Yes. I think I finally understand your question. If you post your code for extracting the room name I'll try to put together a pull request with a more automatic way. – paulmelnikow Feb 02 '13 at 21:52
  • i edited my question.please check..in createAndEnterRoom:Message method,i create the room with the name of the room from which i received invitation,cuz i coud not find any method which allows me to enter the room from which i received invitation directly and also handle that room's delegate method like xmppRoom:didJoin etc. – Vishal Singh Feb 02 '13 at 22:10
  • alright thanks for the conformation.I was wondering if i am taking a long route to travel small distance, but if thats the only route then m happy.:) – Vishal Singh Feb 03 '13 at 07:18
-1

just add below code

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

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

     [xmppRoster acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];

 //For reject button
//     [xmppRoster rejectPresenceSubscriptionRequestFrom:[tmpPresence from]];          
}

inside the method

 - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence ;
method
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Awais Mobeen
  • 733
  • 11
  • 19