2012-04-21 56 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的服務器)