简体   繁体   中英

How to connect XMPP server through XMPPFramework in ios in objective c

I am connecting to XMPP server through XMPPFramework in IOS in objective C, I had initialized the connection parameter in viewDidLoad method like this:

- (void)viewDidLoad {
[super viewDidLoad];
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

xmppStream.hostName = @"hostname";
xmppStream.hostPort = 5222;

NSString *username = @"name@domainname.net";
NSString *password = @"123456";

[xmppStream setMyJID:[XMPPJID jidWithString:username]];

NSError *error = nil;
if (![xmppStream oldSchoolSecureConnectWithTimeout: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];
}
}

And Trying to Authenticate in on button click like this:

- (IBAction)connectToXmpp:(id)sender {

NSLog(@"%hhd", [xmppStream isConnected]);
NSError *error = nil;

if (![xmppStream authenticateWithPassword:@"123456" error:&error]) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:[NSString stringWithFormat:@"Can't authenticate %@", [error localizedDescription]]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}
[xmppStream sendElement:[XMPPPresence presence]];
}

But getting Error message on button click here is the error message:

在此处输入图片说明

Can some one please help me.Thanks.

@prem nath

In Above code you are trying to connect to server in - (void)viewDidLoad . But You can authenticate with password after server connection established.

So - (void)xmppStreamDidConnect:(XMPPStream *)sender of XMPPStream Delegate is called when connection is established. You have to authenticate with server in XMPPStream Delegate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM