Methods for new user registration xmpp framework iOS

夙愿已清 提交于 2019-12-20 12:25:22

问题


I have developed the XMPP Chat client for iOS and now I'm researching for how to do a new user registration from iOS itself. Can anyone help with the methods used to register a new user. As it needs to communicate with the Server and store the username and password to the server database. Please help I'm searching it from 2 days.


回答1:


This solution HAS WORKED for me

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:


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

We will know whether the registration is successful or not using the below delegates.

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



回答3:


New user can register at XMPP server from iOS by two methods as

Methode 1.) By In band Registration (In-band registration means that users that do not have an account on your server can register one using the XMPP protocol itself, so the registration stays "in band", inside the same protocol you're already using.) you have to use XEP-0077 extension.

And your server should also have to support In band Registration.

Use these steps for In band Registration

step 1: connect with 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 declare at @interface part of your file

step 2: When xmppStream Delegate - (void)xmppStreamDidConnect:(XMPPStream *)sender call

step 3: Start register via In-Band Registration as

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

step 4: Check registration success or failure by XMPPStream delegate

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

Methode 2.) By XMPP Rest Api on the openFire server installed a plugin (Rest Api plugin) that allows normal registration.

Use these steps for Rest Api Registration

step 1: Install Rest Api plugin on server

step 2: Configure server for Rest Api as server -> server settings -> Rest Api then enable it.

You can use "Secret key auth" for secure user registration, so copy that from openfire server and use when rest api is called for registration.

step 3: Call Rest Api for registration

-(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 is a Rest api url string.

AuthenticationToken is a "Secret key auth" (copy from openfire server)



来源:https://stackoverflow.com/questions/10257557/methods-for-new-user-registration-xmpp-framework-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!