本文介绍了我如何在 ios 的 XMPPFrameWork 中创建一个新的用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

但是通过这种方法我无法在服务器上注册.它让我连接到 Xmppstream 请稍等.. 谁能帮助我如何在 ios 中使用 XMPPFrameWork 在服务器上创建一个新帐户.

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.

推荐答案

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

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

这篇关于我如何在 ios 的 XMPPFrameWork 中创建一个新的用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 18:42