繁体   English   中英

如何在目标C中的ios中通过XMPPFramework连接XMPP服务器

[英]How to connect XMPP server through XMPPFramework in ios in objective c

我正在通过目标C中的IOS中的XMPPFramework连接到XMPP服务器,我已经在viewDidLoad方法中初始化了连接参数,如下所示:

- (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];
}
}

并尝试在按钮上进行身份验证,如下所示:

- (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]];
}

但是单击按钮获取错误消息是错误消息:

在此处输入图片说明

有人可以帮我吗,谢谢。

@prem nath

在上述代码中,您尝试连接到- (void)viewDidLoad服务器。 但是建立服务器连接后,可以使用密码进行身份验证。

因此- (void)xmppStreamDidConnect:(XMPPStream *)sender建立连接时将调用- (void)xmppStreamDidConnect:(XMPPStream *)sender XMPPStream Delegate - (void)xmppStreamDidConnect:(XMPPStream *)sender 您必须通过XMPPStream委托中的服务器进行身份验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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