Search code examples
iosxmppxcode5xmppframework

Authenticate Anonymously using XMPP framework in iOS


I want to connect to openfire server anonymously using xmpp framework in iOS. I can connect to open fire by providing the JID and PW. However when I tried to connect anonymously it says "The server doesn't support anonymous authentication".

I'm using a button in xib file. When its clicked below code executes:

- (IBAction)login:(id)sender {

    [[self appDelegate]connect];

    NSError *authenticationError = nil;
    [self.xmppStream authenticateAnonymously:&authenticationError];
}

and below is the code for connect method:

- (BOOL)connect {

    [self setupStream];
    xmppStream.hostName = @"abc.xyz.com";
    //xmppStream.hostName = @"Virtuals-MacBook-Pro.local ";

    NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];
    NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"userPassword"];

    if (![xmppStream isDisconnected]) {
        return YES;
    }

    if (jabberID == nil || myPassword == nil) {

        return NO;
    }

    [xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
    password = myPassword;

    NSError *error = nil;
    if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[NSString stringWithFormat:@"Can't connect to server %@", [error localizedDescription]]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
        //[alertView release];


        return NO;
    }

    return YES;
}

Solution

  • Steps for anonymous authentication:

    1- First of all connect the xmpp-stream, then authenticate anonymously.

    [[self xmppStream] authenticateAnonymously:&error];
    

    then you will be authenticated anonymously. but a very important thing. before authenticating, get the username and password of a registered user and connect to the xmpp-stream