Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 614
Mughees Musaddiq Avatar asked Mar 25 '14 14:03

Mughees Musaddiq


People also ask

What is XMPP for Objective-C?

An XMPP Framework in Objective-C for the Mac and iOS development community. XMPPFramework provides a core implementation of RFC-3920 (the XMPP standard), along with the tools needed to read & write XML. It comes with multiple popular extensions (XEP's), all built atop a modular architecture, allowing you to plug-in any code needed for the job.

How do I install XMPP framework in Xcode?

The easiest way to install XMPPFramework is using CocoaPods. To integrate XMPPFramework into your Xcode project using Carthage, specify it in your Cartfile: Run carthage to build the framework and drag the built XMPPFramework.framework into your Xcode project.

What is xmppframework?

XMPPFramework provides a core implementation of RFC-3920 (the XMPP standard), along with the tools needed to read & write XML. It comes with multiple popular extensions (XEP's), all built atop a modular architecture, allowing you to plug-in any code needed for the job. Additionally the framework is massively parallel and thread-safe.

How do I enable anonymous sign-in in Firebase?

In the Firebase console, open the Auth section. On the Sign-in Methods page, enable the Anonymous sign-in method. When a signed-out user uses an app feature that requires authentication with Firebase, sign in the user anonymously by completing the following steps:


2 Answers

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

like image 82
Hassaan Rabbani Avatar answered Sep 28 '22 06:09

Hassaan Rabbani


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)

like image 34
PakitoV Avatar answered Sep 28 '22 06:09

PakitoV