新用户注册方法xmpp框架iOS

时间:2012-04-21 08:52:43

标签: ios xmpp chat registration

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

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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

答案 2 :(得分:0)

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

Methode 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委派- (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。)通过XMPP Rest Api在openFire服务器上安装了一个允许正常注册的插件(Rest Api插件)。

将这些步骤用于Rest Api Registration

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

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

您可以使用“密钥验证”进行安全用户注册,因此请从openfire服务器复制该密码并在调用rest api进行注册时使用。

第3步:呼叫休息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服务器复制)

相关问题