Failed to connect XMPPFramework to Openfire server in iOS

I am working on an iOS chat application where the user enters the username into the application. I downloaded XMPPFramework from GitHub XMPPFramework . I am trying to connect an XMPP framework with an Openfire server following this tutorial . Here is my code for connecting 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 start the application, it shows a warning can't connect to server . I checked a lot of questions about StackOverflow and tried to search the Internet, but did not find a suitable solution. How to connect it to the Openfire service? If I am doing something wrong in my code, offer me a piece of code or a tutorial for this to happen.

+2
source share
2 answers

Lots of features.

Try adding breakpoints to xmppStreamDidConnect and xmppStreamDidAuthenticate .

If xmppStreamDidConnect not reached, the connection is not established; You must correct your host name.

If xmppStreamDidAuthenticate not reached, the user is not authenticated; You must correct your credentials, i.e. username and / or password.

One common mistake is the @domainname exception at the end of the username, i.e. username@domainname , for example. keithoys@openfireserver , where the domain name is openfireserver .

+2
source

Hope this is still relevant, if not, we hope this helps others. There are some problems in the code:

  • I do not see a 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]; } 
  • Most XMPP APIs are asynchronous.
    You must set the thread delegate to receive events. Check out XMPPStreamDelegate and XMPPStream#addDelegate

If you do not want to pass the XMPPStream.h code yourself , you can implement all the XMPPStreamDelegate methods and log events. This will help you understand how the structure works.

Hope this helps, Yaron

+2
source

Source: https://habr.com/ru/post/1433164/


All Articles