无法在目标c中注册来自ejabbered xmpp的新用户

时间:2015-05-25 05:34:17

标签: ios objective-c iphone xmpp ejabberd

在SignupViewController中

if (![[[self appdelegate] xmppStream] isConnected]) {
            [[self appdelegate] setupStream];
            [[self appdelegate]connect] && [[self appdelegate].xmppStream supportsInBandRegistration];

        } else {
            [[self  appdelegate] registerWithElements];
        }

在AppDelegate.m中

- (void)registerWithElements {
    NSError *error = nil;
    NSMutableArray *elements = [NSMutableArray array];
    [elements addObject:[NSXMLElement elementWithName:@"username" stringValue:[UserDefaultsManager getUsername]]];
    [elements addObject:[NSXMLElement elementWithName:@"password" stringValue:[UserDefaultsManager getPassword]]];
    [elements addObject:[NSXMLElement elementWithName:@"email" stringValue:[UserDefaultsManager getUsername]]];

    [[self xmppStream] registerWithElements:elements error:&error];
}

它总是在didNotRegister方法中。请提供合适的解决方案以便成功注册。

1 个答案:

答案 0 :(得分:1)

希望这会对你有所帮助:

- (AppDelegate *)appDelegate
{
    return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}

- (XMPPStream *)xmppStream {
    return [[self appDelegate] xmppStream];
}

-(void)registerUser {
    NSString *myJID = userName;
    NSString *myPassword = 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.
    //
    //   myJID = @"raj@h.local";
    //   myPassword = @"123";

    //if (myJID == nil || myPassword == nil) {
    //    return ;
    //}

    [_xmppStreamM setMyJID:[XMPPJID jidWithString:myJID]];
    //password = myPassword;

    NSError *error = nil;

    if (![_xmppStreamM connectWithTimeout:XMPPStreamTimeoutNone error:&error])
        if (myJID == nil || myPassword == nil) {
            return ;;
        }

    [_xmppStreamM setMyJID:[XMPPJID jidWithString:myJID]];
    //_password = myPassword;

    //NSError *error = nil;
    if (![_xmppStreamM connectWithTimeout:XMPPStreamTimeoutNone error:&error])
        [[[self appDelegate] xmppStream] connectWithTimeout:XMPPStreamTimeoutNone error:nil];

    //_userName = self.userName.text;
    //  NSString *password = self.password.text;
    //    NSString *name =@"raj";
    //
    //
    //    NSString *email=@"raj@gmail.com";


    NSMutableArray *elements = [NSMutableArray array];

    [elements addObject:[NSXMLElement elementWithName:@"username" stringValue:@"username"]];
    [elements addObject:[NSXMLElement elementWithName:@"password" stringValue:@"password"]];

    [[[self appDelegate] xmppStream] registerWithElements:elements error:nil];
    NSLog(@"Register ====%@",[[self appDelegate] xmppStream]);
    NSLog(@"Register ====%@",elements);
}