新用户注册的XMPP架构的iOS方法(Methods for new user registrati

2019-07-29 02:16发布

我已经开发了适用于iOS的XMPP聊天客户端,现在我研究了如何从自身的iOS做一个新用户的注册。 任何人都可以帮助用来注册一个新用户的方法。 因为它需要与服务器进行通信,并存储到服务器数据库的用户名和密码。 请帮我2天寻找它。

Answer 1:

该解决方案为我工作

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


Answer 2:

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];
}


Answer 3:

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

1.了Methode)通过带内注册(带内注册意味着没有你的服务器上的帐户的用户可以使用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

梅索德2.)通过XMPP REST API的Openfire的服务器上安装了插件(REST API插件),允许正常注册。

按照这些步骤REST API注册

步骤1:在服务器上安装REST API插件

步骤2:REST API配置服务器作为服务器- >服务器设置- > REST API然后启用它。

您可以使用“秘密钥匙权威性”为安全用户注册,所以复制从服务器的Openfire当REST API调用注册使用。

步骤3:调用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的服务器)



文章来源: Methods for new user registration xmpp framework iOS