Question

Hi I have tried some code for new user registration using XMPPFrameWork in IOS.

    -(IBAction)Registaton:(id)sender
{
//    iPhoneXMPPAppDelegate *appDelegate =(iPhoneXMPPAppDelegate *)[[UIApplication sharedApplication]delegate];

    [[[self appDelegate] xmppStream] connectWithTimeout:XMPPStreamTimeoutNone error:nil];
    NSString *username = self.username.text;
    NSString *password = self.password.text;
    NSString *name =self.name.text;
    NSString *email=self.email.text;

    NSMutableArray *elements = [NSMutableArray array];
    [elements addObject:[NSXMLElement elementWithName:@"username" stringValue:username]];
    [elements addObject:[NSXMLElement elementWithName:@"password" stringValue:password]];
    [elements addObject:[NSXMLElement elementWithName:@"name" stringValue:name]];

    [elements addObject:[NSXMLElement elementWithName:@"email" stringValue:email]];

    [[[self appDelegate] xmppStream] registerWithElements:elements error:nil];
    NSLog(@"Register ====%@",[[self appDelegate] xmppStream]);

   //[[[self appDelegate] xmppStream] registerWithPassword:elements error:nil];

}

but through this method i am not able register on server. it gives me connecting to Xmppstream Please wait.. Can anyone help me how can i create a new account on server using XMPPFrameWork in ios.

Was it helpful?

Solution

Try this. Its works fine for me

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

OTHER TIPS

A simple and WORKING approach which 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top