[英]How to set up anonymous login with XMPPFramework - iOS Objective-C
我正在尝试设置匿名登录,这样我的用户就不必在eJabberd服务器上创建帐户即可使用聊天室。 ejabberd.cfg中服务器的配置为:
{host_config, "bubble", [{auth_method, anonymous},
{anonymous_protocol, login_anon}]}.
我将客户端连接到XMPPStream的方法:
- (BOOL)connect {
[self setupStream];
if (![self.xmppStream isDisconnected]) {
return YES;
}
if (![PFUser currentUser]) {
return NO;
}
NSString *currentUserId = [NSString stringWithFormat:@"%@@bubble",[PFUser currentUser].objectId];
[self.xmppStream setMyJID:[XMPPJID jidWithString:currentUserId]];
self.xmppStream.hostName = kJABBER_HOSTNAME;
NSError *error = nil;
if (![self.xmppStream connectWithTimeout:10 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];
return NO;
}
return YES;
}
以及xmppStreamDidConnect方法:
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
self.isOpen = YES;
NSError *error = nil;
if(![self.xmppStream authenticateAnonymously:&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];
}
}
当我尝试登录服务器时,它继续显示“服务器不支持匿名身份验证”。
不确定我在这里做错了什么,请让我知道您的想法。
从XMPPFramework文档中:
如果希望使用匿名身份验证,则仍应在调用connect之前设置myJID。 您可以简单地将其设置为“ anonymous @ domain”之类的内容 ,其中“ domain”是适当的域。 认证过程之后,您可以查询myJID属性以查看分配的JID是什么。
确保服务器上的设置也允许匿名登录。
如果您无权访问服务配置,则仍然可以使用以下方法检查服务器是否允许匿名登录:
- (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");
}
}
确保将其放置在didConnect委托方法中,因为它需要首先连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.