Unable to connect XMPPFramework to Openfire server

2020-04-14 08:32发布

I am working on an iOS chat app where user login to app. I've downloaded XMPPFramework from GitHub XMPPFramework. I am trying to connect XMPP framework with Openfire server by following this tutorial. Here is my code to connect XMPP to openfire.

- (BOOL)connect {

    [self setupStream];
    [xmppStream setHostName:@"192.168.1.5"];
    [xmppStream setHostPort:5222];

    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 isConnected])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[NSString stringWithFormat:@"Can't connect to server %@", [error localizedDescription]]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
        return NO;
    }
    return YES;
}

The problem is when I run the app, it shows the alert can't connect to server. I have checked many questions on StackOverflow and tried googling but couldn't find any relevant solution. How to connect it to the Openfire serve? If I am doing anything wrong in my code please suggest me with a snippet of code or a tutorial to make this happen.

2条回答
来,给爷笑一个
2楼-- · 2020-04-14 08:58

Hope this still relevant, if not, hopefully it will help others. There are some issues with your code:

  1. I don't see the call to connect, you should add something like this:

    NSError *error = nil;
    if (![_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) { 
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"
                                                            message:@"Msg"
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];       
    }
    
  2. Most of the XMPP API is asynchronous.
    You have to set the stream delegate in order to receive events. Check out XMPPStreamDelegate and XMPPStream#addDelegate

If you don't want to go through the code yourself XMPPStream.h , you can implement all methods of XMPPStreamDelegate and log the events. This will help you understand how the framework works.

Hope this helps, Yaron

查看更多
可以哭但决不认输i
3楼-- · 2020-04-14 09:18

A host of possibilities.

Try adding break points at xmppStreamDidConnect and xmppStreamDidAuthenticate.

If xmppStreamDidConnect isn't reached, the connection is not established; you've to rectify your hostName.

If xmppStreamDidAuthenticate isn't reached, the user is not authenticated; you've to rectify your credentials i.e. username and/or password.

One common mistake is omitting of @domainname at the back of username i.e. username@domainname e.g. keithoys@openfireserver where domain name is openfireserver.

查看更多
登录 后发表回答