Question

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;
}
Was it helpful?

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

OTHER TIPS

The accepted answer is almost right but is mixing things (connection and authentication)

What you are facing is probably a server side configuration problem, if your server does not allow you to log in anonymously you cannot, period.

Anyway you can still try to connect anonymously and handle the fact that you are not allow, for that you need to:

1) Set your JabberID to anonymous@domain (were domain is your server domain)

[self.xmppStream setMyJID:[XMPPJID jidWithString:@"anonymous@domain"]];

2) With that in place you can connect to the server (you do not need a valid user as the accepted answer pointed out)

[self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]

3) Once you get response from the sever your XMPP delegate method didConnect will be called, in there you check if the server configuration supports anonymous authentication and if so try to authenticate anonymously

- (void)xmppStreamDidConnect:(XMPPStream*)sender
{
    self.isXmppConnected = YES;

    if ([self.xmppStream supportsAnonymousAuthentication]) {
        NSError* error = nil;
        //the server does support anonymous auth
        [self.xmppStream authenticateAnonymously:&error];
    }
    else {
        NSLog(@"The server does not support anonymous authentication");
    }
}

4) You handle however you want the situations were the server does not support anonymous auth (maybe trie with a well know user or display a warning to the user) or you get an error authenticating (network issues)

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