新用户注册的方法xmpp框架iOS

我开发了适用于iOS的XMPP聊天客户端,现在我正在研究如何从iOS本身进行新的用户注册。 任何人都可以帮助用于注册新用户的方法。 因为它需要与服务器通信并将用户名和密码存储到服务器数据库。 请帮助我从2天内search它。

这个解决scheme已经为我工作了

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

下面的代表我们会知道注册是否成功。

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

新用户可以通过两种方法从iOS注册到XMPP服务器

方法1.)通过带内注册(带内注册意味着您的服务器上没有帐户的用户可以使用XMPP协议本身进行注册,因此注册保持“带内”,在您使用的同一协议已经在使用。)你必须使用XEP-0077扩展。

而你的服务器也应该支持In band Registration。

使用这些步骤进行带内注册

第1步:连接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在文件的@interface部分声明

第2步:当xmppStream委托- (void)xmppStreamDidConnect:(XMPPStream *)sender调用

步骤3:通过带内注册开始注册为

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

步骤4:通过XMPPStream委托检查注册成功或失败

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

Methode 2.)通过XMPP Rest Api在openFire服务器上安装了一个允许正常注册的插件(Rest Api插件)。

使用这些步骤进行Rest Api注册

第1步:在服务器上安装Rest Api插件

步骤2:configuration服务器Rest Api作为服务器 – >服务器设置 – > Rest Api然后启用它。

您可以使用“密钥身份validation”进行安全的用户注册,因此请从openfire服务器复制该密钥,并在restAPI使用时进行注册。

步骤3:呼叫Rest Api进行注册

 -(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是Rest apiurlstring。

AuthenticationToken是一个“密钥validation”(从openfire服务器复制)