简体   繁体   English

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

[英]How to set up anonymous login with XMPPFramework - iOS Objective-C

I'm trying to set up an anonymous login so my users don't have to create an account an account on the eJabberd server to use the chat room. 我正在尝试设置匿名登录,这样我的用户就不必在eJabberd服务器上创建帐户即可使用聊天室。 The configuration for the server in the ejabberd.cfg is: ejabberd.cfg中服务器的配置为:

{host_config, "bubble", [{auth_method, anonymous},
                         {anonymous_protocol, login_anon}]}.

My method to connect the client to the XMPPStream: 我将客户端连接到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;
}

As well as the xmppStreamDidConnect method: 以及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];
    }
}

When I try to login into the server, it keep on getting "The server does no support anonymous authentication". 当我尝试登录服务器时,它继续显示“服务器不支持匿名身份验证”。

Not sure what I'm doing wrong here, please let me know your thoughts. 不确定我在这里做错了什么,请让我知道您的想法。

From the XMPPFramework documentation: 从XMPPFramework文档中:

If you wish to use anonymous authentication, you should still set myJID prior to calling connect. 如果希望使用匿名身份验证,则仍应在调用connect之前设置myJID。 You can simply set it to something like "anonymous@domain" , where "domain" is the proper domain. 您可以简单地将其设置为“ anonymous @ domain”之类的内容 ,其中“ domain”是适当的域。 After the authentication process, you can query the myJID property to see what your assigned JID is. 认证过程之后,您可以查询myJID属性以查看分配的JID是什么。

Make sure that the settings on the server allow anonymous login as well. 确保服务器上的设置也允许匿名登录。

If you don't have access to the serve configuration you can still check if the server allows anonymous login using something like: 如果您无权访问服务配置,则仍然可以使用以下方法检查服务器是否允许匿名登录:

- (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");
    }
}

Be sure to place it in the didConnect delegate method, as it needs to be connected first. 确保将其放置在didConnect委托方法中,因为它需要首先连接。

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

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