2012-04-21 58 views
12

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

+0

提起你使用会使得很大的区别的服务器。 – ggozad 2012-04-21 15:59:16

+0

我使用的OpenFire服务器,虽然我研究了这一点,用于注册在xmpp框架库中的方法是 - (BOOL)supportsInBandRegistration; - (BOOL)registerWithPassword:(NSString *)密码错误:(NSError **)errPtr;但是在寻找如何实现它。 – obaid 2012-04-22 06:09:12

+1

看看我的答案在这里:http://stackoverflow.com/questions/9988206/new-registration-on-openfire-with-strophe-js/10000927#10000927 – ggozad 2012-04-23 15:06:46

回答

9

该溶液很适合我

NSString *username = @"[email protected]_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 
+0

嘿,我试过你的代码,但没有为我工作。我无法将XMPP连接到openfire。请帮助我知道如何做到这一点。我正在寻找1周的答案,但没有得到任何帮助。请让我知道如何配置XMPP。 @rohit mandiwal。 – 2014-11-11 10:02:16

+0

如果您还没有服务器上的帐户,有没有办法做到这一点? – sudo 2015-06-30 23:21:22

+0

没有工作..... – 2015-09-21 10:49:30

10
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:@"[email protected]"]]; 

[[[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]; 
} 
+0

嗨Karun你在哪里设置主机名和端口名? – 2014-07-30 11:43:04

+0

嗨兄弟, - (void)xmppStreamDidRegister:(XMPPStream *)发送方法不调用。我应该如何检查用户是否已注册 – 2015-09-21 07:53:26

+0

不能正常工作.... – 2015-09-21 09:07:41

0

新用户通过两种方法作为

了Methode 1)通过在频带登记(IN-在从iOS的XMPP服务器可以注册带注册意味着您的服务器上没有帐户的用户可以使用XMPP协议本身进行注册,因此注册保持“带内”状态,在您已经使用的相同协议内)。必须使用XEP -0077扩展名。

而你的服务器也应该支持带内注册。

按照这些步骤在频带登记

第1步:连接与xmppStream

- (BOOL)connectAndRegister 
{ 
    if (![xmppStream isDisconnected]) { 
     return YES; 
    } 

    NSString *myJID = @"[email protected]_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 = @"[email protected]/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在申报文件

步骤2中的@interface部分:当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:REST API配置服务器作为服务器 - >服务器设置 - > REST API然后启用它。

您可以使用“秘密钥匙权威性”为安全用户注册,所以复制从服务器的Openfire当REST API调用注册使用。

步骤3:呼叫REST API登记

-(void)CreateUserAPI 
{ 
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"abc",@"username",@"SOME_PASSWORD",@"password",@"abc-nickname",@"name",@"[email protected]",@"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 API URL字符串。

AuthenticationToken是“秘密密钥身份验证”(副本从Openfire的服务器)