如何使用XMPPFramework设置匿名登录 - iOS Objective-C

时间:2015-07-30 18:09:49

标签: ios xmppframework

我正在尝试设置匿名登录,因此我的用户无需在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];
    }
}

当我尝试登录服务器时,它继续“服务器不支持匿名身份验证”。

不确定我在这里做错了什么,请让我知道你的想法。

1 个答案:

答案 0 :(得分:0)

来自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委托方法中,因为它需要先连接。