如何在ios中的XMPPFrameWork中创建新的用户帐户

时间:2013-12-26 11:04:53

标签: ios registration xmppframework

您好我在IOS中使用XMPPFrameWork尝试了一些新用户注册的代码。

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

}

但是通过这种方法,我无法在服务器上注册。它让我连接到Xmppstream请稍候..任何人都可以帮助我如何使用ios中的XMPPFrameWork在服务器上创建一个新帐户。

2 个答案:

答案 0 :(得分:0)

试试这个。它对我来说很好用

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

答案 1 :(得分:0)

为我工作的一种简单而有效的方法

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