XMPPFramework - How to register a new user account

2019-07-01 17:57发布

问题:

I'm trying to use XMPPFramework to connect to an Openfire server and create a new user account. If I am already logged in as a different user, this code will create a new user account:

NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:register"];
[query addChild:[NSXMLElement elementWithName:@"username" stringValue:userName]];
[query addChild:[NSXMLElement elementWithName:@"password" stringValue:pswd]];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"set"];
[iq addAttributeWithName:@"id" stringValue:@"reg2"];
[iq addChild:query];
[xmppStream sendElement:iq];

Now what I want to do is make a connection to the server and create the user account WITHOUT first being connected as a different user. I've tried setting the hostname in xmppStream and making a connection (as below), but that fails to connect.

NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:register"];
[query addChild:[NSXMLElement elementWithName:@"username" stringValue:userName]];
[query addChild:[NSXMLElement elementWithName:@"password" stringValue:pswd]];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"set"];
[iq addAttributeWithName:@"id" stringValue:@"reg2"];
[iq addChild:query];
[xmppStream sendElement:iq];

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

Any ideas on how can I get this to connect and register the new user?

回答1:

uses this code.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


 ///  [[self appDelegate] connect];
    AppDelegate *delegate=[self appDelegate];
    [delegate connect];


    useSSL=NO;
    allowSelfSignedCertificates=NO;
    allowSSLHostNameMismatch=NO;
    customCertEvaluation=NO;
    isRegistering=NO;

    self.xmppStream= [[self appDelegate] xmppStream];

    BOOL *connect=[self.xmppStream isConnected];




    [self createAccount];
}

    - (void)updateAccountInfo
    {


        //NSString *domain = [[NSString alloc] initWithString:@"192.168.1.100"];

        //int port = 5222;
        NSString *usname =[[NSString alloc] initWithString:self.txtUsername.text];
        NSString *juser =[[NSString alloc] initWithString:[usname stringByAppendingString:@"your server ip"]];

        XMPPJID *jid = [XMPPJID jidWithString:juser];
        [self  xmppStream].myJID =jid;

        allowSelfSignedCertificates =  NSOnState;
        allowSSLHostNameMismatch    =  NSOnState;
        NSUserDefaults *dflts = [NSUserDefaults standardUserDefaults];

        //[dflts setObject:domain forKey:@"Account.Server"];

    //  [dflts setObject:(port ? [NSNumber numberWithInt:port] : nil)
            //    forKey:@"Account.Port"];

        [dflts setObject:juser
                  forKey:@"Account.JID"];
        [dflts setObject:@"ios"
                  forKey:@"Account.Resource"];

        [dflts setBool:useSSL                      forKey:@"Account.UseSSL"];
        [dflts setBool:allowSelfSignedCertificates forKey:@"Account.AllowSelfSignedCert"];
        [dflts setBool:allowSSLHostNameMismatch    forKey:@"Account.AllowSSLHostNameMismatch"];
        [dflts setBool:YES forKey:@"Account.RememberPassword"];

        [dflts setObject:self.txtPasswd.text forKey:@"Account.Password"];
        [dflts synchronize];



    }

    - (void)createAccount
    {
        [self updateAccountInfo];

        NSError *error = nil;
        BOOL success;

        if(![[[self appDelegate] xmppStream] isConnected])
        {

            if (useSSL)
                success = [[self  xmppStream] oldSchoolSecureConnectWithTimeout:XMPPStreamTimeoutNone error:&error];
            else
                success = [[self xmppStream] connectWithTimeout:XMPPStreamTimeoutNone error:&error];
        }
        else
        {
            //NSString *password = [[NSString alloc] initWithString:@"321" ];

            success = [[self xmppStream] registerWithPassword:self.txtPasswd.text error:&error];
        }

        if (success)
        {
            [self appDelegate].isRegistering = YES;
        }
        else
        {
            NSLog(@"not succeed ");

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


        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration" message:@"Registration with XMPP 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 with XMPP   Failed!" message:regError delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

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

            [alert setMessage:@"Username Already Exists!"];
        }
        [alert show];
         }