【问题标题】:Authenticate Anonymously using XMPP framework in iOS在 iOS 中使用 XMPP 框架进行匿名身份验证
【发布时间】:2014-05-03 12:25:16
【问题描述】:

我想在 iOS 中使用 xmpp 框架匿名连接到 openfire 服务器。我可以通过提供 JID 和 PW 来连接到 open fire。但是,当我尝试匿名连接时,它显示“服务器不支持匿名身份验证”。

我正在使用 xib 文件中的按钮。当它点击下面的代码执行时:

- (IBAction)login:(id)sender {

    [[self appDelegate]connect];

    NSError *authenticationError = nil;
    [self.xmppStream authenticateAnonymously:&authenticationError];
}

下面是connect方法的代码:

- (BOOL)connect {

    [self setupStream];
    xmppStream.hostName = @"abc.xyz.com";
    //xmppStream.hostName = @"Virtuals-MacBook-Pro.local ";

    NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];
    NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"userPassword"];

    if (![xmppStream isDisconnected]) {
        return YES;
    }

    if (jabberID == nil || myPassword == nil) {

        return NO;
    }

    [xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
    password = myPassword;

    NSError *error = nil;
    if (![xmppStream connectWithTimeout: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];
        //[alertView release];


        return NO;
    }

    return YES;
}

【问题讨论】:

    标签: ios xmpp xcode5 xmppframework


    【解决方案1】:

    匿名认证步骤:

    1- 首先连接 xmpp-stream,然后匿名验证。

    [[self xmppStream] authenticateAnonymously:&error];
    

    那么您将获得匿名身份验证。但是一件很重要的事情。在认证之前,获取注册用户的用户名和密码并连接到xmpp-stream

    【讨论】:

      【解决方案2】:

      接受的答案几乎是正确的,但混合了一些东西(connectionauthentication

      您面临的可能是服务器端配置问题,如果您的服务器不允许您匿名登录,则不能,句号。

      无论如何,您仍然可以尝试匿名连接并处理不允许的事实,因为您需要:

      1) 将您的 JabberID 设置为 anonymous@domain(如果 domain 是您的服务器域)

      [self.xmppStream setMyJID:[XMPPJID jidWithString:@"anonymous@domain"]];
      

      2) 有了它,您可以 连接 到服务器(您不需要有效用户,正如接受的答案指出的那样)

      [self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]
      

      3) 一旦你得到服务器的响应,你的 XMPP 委托方法 didConnect 将被调用,在那里你检查服务器配置是否支持匿名身份验证,如果支持,请尝试匿名 身份验证

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

      4) 您可以按照自己的意愿处理服务器不支持匿名身份验证的情况(可能与知名用户一起尝试或向用户显示警告)或者您在验证时遇到错误(网络问题)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-28
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多