12

我已经为 iOS 开发了 XMPP 聊天客户端,现在我正在研究如何从 iOS 本身进行新用户注册。任何人都可以帮助使用注册新用户的方法。因为它需要与服务器通信并将用户名和密码存储到服务器数据库中。请帮助我从 2 天开始搜索它。

4

3 回答 3

10
NSMutableArray *elements = [NSMutableArray array];
[elements addObject:[NSXMLElement elementWithName:@"username" stringValue:@"venkat"]];
[elements addObject:[NSXMLElement elementWithName:@"password" stringValue:@"dfds"]];
[elements addObject:[NSXMLElement elementWithName:@"name" stringValue:@"eref defg"]];
[elements addObject:[NSXMLElement elementWithName:@"accountType" stringValue:@"3"]];
[elements addObject:[NSXMLElement elementWithName:@"deviceToken" stringValue:@"adfg3455bhjdfsdfhhaqjdsjd635n"]];

[elements addObject:[NSXMLElement elementWithName:@"email" stringValue:@"abc@bbc.com"]];

[[[self appDelegate] xmppStream] registerWithElements:elements error:nil];

我们将通过以下代表知道注册是否成功。

- (void)xmppStreamDidRegister:(XMPPStream *)sender{


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration" message:@"Registration Successful!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}


- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error{

    DDXMLElement *errorXML = [error elementForName:@"error"];
    NSString *errorCode  = [[errorXML attributeForName:@"code"] stringValue];   

    NSString *regError = [NSString stringWithFormat:@"ERROR :- %@",error.description];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration Failed!" message:regError delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    if([errorCode isEqualToString:@"409"]){        

        [alert setMessage:@"Username Already Exists!"]; 
    }   
    [alert show];
}
于 2013-09-25T09:30:49.137 回答
9

这个解决方案对我有用

NSString *username = @"rohit@XMPP_SERVER_IP_HERE"; // OR [NSString stringWithFormat:@"%@@%@",username,XMPP_BASE_URL]]
NSString *password = @"SOME_PASSWORD";

AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];

del.xmppStream.myJID = [XMPPJID jidWithString:username];

NSLog(@"Does supports registration %ub ", );
NSLog(@"Attempting registration for username %@",del.xmppStream.myJID.bare);

if (del.xmppStream.supportsInBandRegistration) {
    NSError *error = nil;
    if (![del.xmppStream registerWithPassword:password error:&error])
    {
        NSLog(@"Oops, I forgot something: %@", error);
    }else{
        NSLog(@"No Error");
    }
}

// You will get delegate called after registrations in either success or failure case. These delegates are in XMPPStream class
// - (void)xmppStreamDidRegister:(XMPPStream *)sender
//- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
于 2014-10-03T07:55:47.723 回答
0

新用户可以通过两种方法从 iOS 注册到 XMPP 服务器:

方法1.)通过带内注册(带内注册意味着在您的服务器上没有帐户的用户可以使用 XMPP 协议本身注册一个帐户,因此注册保持“带内”,在您使用的同一协议内已经在使用。)你必须使用XEP-0077扩展。

而且您的服务器还应该支持带内注册。

使用这些步骤进行带内注册

第 1 步:连接 xmppStream

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

    NSString *myJID = @"abc@XMPP_SERVER_IP_HERE"; // OR [NSString stringWithFormat:@"%@@%@",username,XMPP_BASE_URL]]
    NSString *myPassword = @"SOME_PASSWORD";

    //
    // If you don't want to use the Settings view to set the JID,
    // uncomment the section below to hard code a JID and password.
    //
    // Replace me with the proper JID and password:
    //  myJID = @"user@gmail.com/xmppframework";
    //  myPassword = @"";

    if (myJID == nil || myPassword == nil) {
        DDLogWarn(@"JID and password must be set before connecting!");

        return NO;
    }

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

    NSError *error = nil;
    if (![xmppStream connect:&error])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"
                                                            message:@"See console for error details."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];

        DDLogError(@"Error connecting: %@", error);

        return NO;
    }

    return YES;
}

NSString *password在文件的@interface 部分声明

第 2 步:当 xmppStream Delegate- (void)xmppStreamDidConnect:(XMPPStream *)sender调用时

第 3 步:通过带内注册开始注册为

- (void)xmppStreamDidConnect:(XMPPStream *)sender{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    [[NSNotificationCenter defaultCenter] postNotificationName:XMPPStreamStatusDidConnectNotification
                                                        object:nil
                                                      userInfo:nil];
    _isXmppConnected = YES;
    NSError *error = nil;
    DDLogVerbose(@"Start register via In-Band Registration...");

   if (xmppStream.supportsInBandRegistration) {

      if (![xmppStream registerWithPassword:password error:&error]) {
           NSLog(@"Oops, I forgot something: %@", error);
      }else {
          NSLog(@"No Error");
     }
  }
//    [_xmppStream authenticateWithPassword:password error:&error];
}

第 4 步:通过 XMPPStream 委托检查注册成功或失败

- (void)xmppStreamDidRegister:(XMPPStream *)sender
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error

Methode 2.)在openFire服务器上通过XMPP Rest Api安装了一个插件(Rest Api plugin),允许正常注册。

使用这些步骤进行 Rest Api 注册

第 1 步:在服务器上安装 Rest Api 插件

第2步:将Rest Api的服务器配置为服务器->服务器设置->Rest Api,然后启用它。

您可以使用“Secret key auth”进行安全用户注册,因此从 openfire 服务器复制它并在调用 rest api 进行注册时使用。

第三步:调用Rest Api进行注册

-(void)CreateUserAPI
{   
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"abc",@"username",@"SOME_PASSWORD",@"password",@"abc-nickname",@"name",@"abc@example.com",@"email", nil];
    NSData* RequestData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];

    NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:[NSString stringWithFormat:@"%@users",RESTAPISERVER]]];


    [request setHTTPMethod: @"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:AuthenticationToken forHTTPHeaderField:@"Authorization"];
    [request setHTTPBody: RequestData];

    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithRequest:request
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
                // handle response
                if (!error)
                {

                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                    if ([httpResponse statusCode]==201)
                    {

                        NSLog(@"Registration Successful");

                    }else
                    {
                        NSLog(@"Registration failed");
                    }

                }else
                {
                    NSLog(@"Try again for registration");
                }


            }] resume];
}

RESTAPISERVER是一个 Rest api url 字符串。

AuthenticationToken是“秘密密钥身份验证”(来自 openfire 服务器的副本)

于 2017-06-23T13:08:39.910 回答