1

I have been trying to create a XMPPRoom using the below mentioned code, i have looked at various examples online however when i use this code, the delegate xmppRoomDidCreate or xmppRoomDidJoin delegates doesn't get called. I'm not sure what am i doing wrong here?

PS: the delegates of xmppStream do get called, it gets connected and authorized however, the issue is XMPPRoom delegates...

- (void)createChatRoom
{
    NSString *jabberID = @"abcxyz@testservice.com";
    self.xmppStream.hostName = @"testservice.com";


    self.xmppStream = [[XMPPStream alloc]init];
    [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

    [self.xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];

    NSError *error = nil;

    if (![self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[NSString stringWithFormat:@"Cannot connect to server %@",[error localizedDescription]] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];

        return;
    }

    // Configure xmppRoom
    XMPPJID *roomJID = [XMPPJID jidWithString:@"TestRoom@conference.testservice.com"];

    XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];

    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];

    [xmppRoom activate:self.xmppStream];
    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
Keith OYS
  • 2,285
  • 5
  • 32
  • 38
DevCali
  • 398
  • 6
  • 20

2 Answers2

6

Did you add <XMPPRoomDelegate> protocol in the .h file of the view controller in which you want to monitor XMPPRoom's delegates?

It should be something like this: @interface YourViewController : UIViewController <..., XMPPRoomDelegate>.

Of course #import "XMPPRoom.h" should be also in .h file of the mentioned view controller.

ADDITION:

You have to setup XMPPStream object, connect to your 'chat' server (in most cases Jabber server) with XMPPJID, then listen for server response, then authentificate with the password and then to start with XMPPRoom creation if everything mentioned from above goes well.

Example:

- (void)setupStream
{
    NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
    xmppStream = [[XMPPStream alloc] init];

    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

Then to connect to server:

[self setupStream];

NSString *myJID = @"...";

[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];

NSError *error2;
if ([xmppStream connect:&error2])
{
    NSLog(@"Connected to XMPP.");
}
else
{
    NSLog(@"Error connecting to XMPP: %@", [error2 localizedDescription]);
}

Listen for server response (do not forget to add <XMPPStreamDelegate> in .h file):

- (void)xmppStreamDidConnect:(XMPPStream *)sender
{    
    NSError *error;    
    if ([[self xmppStream] authenticateWithPassword:password error:&error])
    {
        NSLog(@"Authentificated to XMPP.");
    }
    else
    {
        NSLog(@"Error authentificating to XMPP: %@", [error localizedDescription]);
    }
}

Listen for server response for authentification status:

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    NSLog(@"%s", __FUNCTION__);
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
    [sender sendElement:presence]; 
}

and then try to create XMPPRoom by calling function after authentification success:

- (void)createChatRoom
{    
    // Configure xmppRoom
    XMPPJID *roomJID = [XMPPJID jidWithString:@"TestRoom@conference.testservice.com"];

    XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];

    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];

    [xmppRoom activate:self.xmppStream];
    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [xmppRoom joinRoomUsingNickname:user history:nil];
}
Sihad Begovic
  • 1,907
  • 1
  • 31
  • 33
  • yes, i have added in .h file, and i can confirm this as after the above code executes, after like a min one of the XMPPRoom protocol gets called - (void)xmppRoomDidLeave:(XMPPRoom *)sender – DevCali Dec 24 '13 at 23:24
  • Do you know if i have to connect and authorize XMPPStream object before attempting to create XMPPRoom? do you have any example? – DevCali Dec 24 '13 at 23:25
  • Also, maybe in your code you need just this line [xmppRoom joinRoomUsingNickname:user history:nil]; where the user is an username of your chat avatar, but I would also implemented these XMPPStream delegates for cases when connection to the server is not successful. – Sihad Begovic Dec 25 '13 at 00:05
  • 1
    Thanks for your detailed answer! I followed all the steps you mentioned in your answer, i can see on eJabbered that the room has been created However, XMPPRoom delegates "didCreate" or "DidJoin" were not fired however, one of XMPPStream delegate - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence got fired with presence type "error" :( – DevCali Dec 25 '13 at 00:13
  • Yes, of course, after authorization you will have to sent your presence to the server (that can be done inside - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender). It would be something like: XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];[[self xmppStream] sendElement:presence]; – Sihad Begovic Dec 25 '13 at 00:26
  • 1
    Also, example from my answer is modified version of the code from this link https://github.com/robbiehanson/XMPPFramework. When you download XMPPFramework library it comes with Xcode project example. – Sihad Begovic Dec 25 '13 at 00:30
  • Hey @Sihad, can you help me with bookmark xmpprooms? do you have any example or documents that can help me? i already read 0048 but that is not very clear on how to bookmark a conference group and then retrieve them back. – DevCali Dec 31 '13 at 02:05
  • here is the actual question link http://stackoverflow.com/questions/20792739/ios-xmpp-frame-work-xep-0048-bookmark-storage – DevCali Dec 31 '13 at 02:09
  • Fetching configuration is missing in your answer. Please refer to http://stackoverflow.com/a/24179388/563735 – Rohit Mandiwal Jul 24 '14 at 01:48
  • Hello All, Thanks you all and starckoverflow, I am able to create group and send Invitations to other with Both Storage (Core data & Memory Storage). Issue is when I create Second group it removes first group data from Core data storage and Also How can we auto join other user ? – Mangesh Oct 03 '14 at 14:30
3

Here is the solution worked for me. You have to implement XMPPRoomDelegate

- (void)createChatRoom
{

    XMPPRoomMemoryStorage *roomStorage = [[XMPPRoomMemoryStorage alloc] init];

    /**
     * Remember to add 'conference' in your JID like this:
     * e.g. uniqueRoomJID@conference.yourserverdomain
     */

    NSString *groupName = [NSString stringWithFormat:@"%@@conference.192.168.0.101",groupNameTextField.text];
    NSLog(@"attempting to create room %@",groupName);
    XMPPJID *roomJID = [XMPPJID jidWithString:groupName];
    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];

    [xmppRoom activate:del.xmppStream];
    [xmppRoom addDelegate:self
            delegateQueue:dispatch_get_main_queue()];

    [xmppRoom joinRoomUsingNickname:[self appDelegate].xmppStream.myJID.user
                            history:nil
                           password:nil];

    NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"groupChatArray"]];
    [array addObject:groupName];

    [[NSUserDefaults standardUserDefaults]setObject:array forKey:@"groupChatArray"];
    [[NSUserDefaults standardUserDefaults]synchronize];


}

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm{
    NSLog(@"didFetchConfigurationForm");
    NSXMLElement *newConfig = [configForm copy];
    NSLog(@"BEFORE Config for the room %@",newConfig);
    NSArray *fields = [newConfig elementsForName:@"field"];

    for (NSXMLElement *field in fields)
    {
        NSString *var = [field attributeStringValueForName:@"var"];
        // Make Room Persistent
        if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
            [field removeChildAtIndex:0];
            [field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
        }
    }
    NSLog(@"AFTER Config for the room %@",newConfig);
    [sender configureRoomUsingOptions:newConfig];
}
Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83