2015-02-24 64 views
0

我正在尝试注册一个新用户到明火,而不使用入站注册,还有一些其他设置是: bool allowSelfSignedCertificates = NO; bool allowSSLHostNameMismatch = NO; bool useSSL = NO。我播种计算器上的几个例子,但他们都不是为我好,还是我没把握的概念...Registert在iOS中使用XMPP的Openfire服务器的新用户

这里是我的代码:

- > .h文件中:

#import <UIKit/UIKit.h> 
#import <CoreData/CoreData.h> 
#import "XMPP.h" 
#import "XMPPRoster.h" 

@interface SignUpViewController : UIViewController <UITextFieldDelegate, UIApplicationDelegate, XMPPRosterDelegate, XMPPStreamDelegate> 
{ 
    XMPPStream *xmppStream; 
} 
@property (nonatomic, strong, readonly) XMPPStream *xmppStream; 

@end 

- > .m文件

- (void)signUpButtonFunction{ 
    NSLog(@"SignUp function"); 

    [[self xmppStream] setHostName:@"IP_ADDRESS"]; 
    [[self xmppStream] setHostPort:5222]; 
    XMPPJID *jid=[XMPPJID jidWithString:emailTextField.text]; 
    [[self xmppStream] setMyJID:jid]; 
    [[self xmppStream] connectWithTimeout:3.0 error:nil]; 

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

    [ xmppStream registerWithElements:elements error:nil]; 

} 


//server connect delegate methods are not working at least it doesn't enter in them 
- (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]; 
} 

这是我使用的库: git library

,也是我想指出的是,我的代码是不是进入委托方法

UPDATE:

  • 改变了signUpButtonFunction到:

    xmppStream = [[XMPPStream alloc] init]; 
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()]; 
    [[self xmppStream] setHostName:@"IP_ADDRESS"]; 
    [[self xmppStream] setHostPort:5222]; 
    [[self xmppStream] setMyJID:[XMPPJID jidWithString:@"[email protected]"]]; 
    [[self xmppStream] connectWithTimeout:XMPPStreamTimeoutNone error:nil]; 
    
    NSMutableArray *elements = [NSMutableArray array]; 
    [elements addObject:[NSXMLElement elementWithName:@"username" stringValue:@"username"]]; 
    [elements addObject:[NSXMLElement elementWithName:@"password" stringValue:@"password"]]; 
    [elements addObject:[NSXMLElement elementWithName:@"name" stringValue:@"eref defg"]]; 
    [elements addObject:[NSXMLElement elementWithName:@"email" stringValue:@"[email protected]"]]; 
    
    [ xmppStream registerWithElements:elements error:nil]; 
    
    NSError *error = nil; 
    if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) 
    { 
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting" 
                    message:@"See console for error details." 
                    delegate:nil 
                  cancelButtonTitle:@"Ok" 
                  otherButtonTitles:nil]; 
        [alertView show]; 
        NSLog(@"%@",error); 
    
  • 而我得到的错误:

错误域= XMPPStreamErrorDomain Cod e = 1“试图在已连接或连接的情况下进行连接。”的UserInfo = {0x7fdc2af1f1c0 = NSLocalizedDescription正在尝试连接时已经连接或连接。}

如果我评论该行:

[ xmppStream registerWithElements:elements error:nil]; 

然后错误消失,但它剧照不进入委托方法。

+0

要连接两次,先删除'[自xmppStream] connectWithTimeout:XMPPStreamTimeoutNone错误:零]' – vitalyster 2015-02-24 12:39:50

+0

其实你需要在注册新用户之前连接流。尝试使用新的用户标识连接到xmppstream。从xmppStreamDidConnect委托中,您将能够知道该流已连接。请尝试从这一点初始化注册。 – QUserS 2015-03-03 12:22:37

回答

1

所以,对此事搜索后,我发现,在Openfire的可安装一个插件,允许正常的注册,所以我已经实现了注册下一个方法:

NSString *urlToCall = @"http://MyIP:9090/plugins/userService/userservice?type=add&secret=BigSecretKey&username=testUser&password=testPass&name=testName&[email protected]"; 
NSURL *url = [NSURL URLWithString:urlToCall]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"GET"]; 
NSError *error = nil; 
NSURLResponse *response; 
NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 
NSString *responseString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; 
if ([responseString isEqual: @"<result>ok</result>\r\n"]) { 
    NSLog(@"user created"); 

} else { 
    NSLog(@"user NOT created"); 
    NSLog(@"%@",responseString); 
} 
0

@Laur斯特凡

首先下载新的演示从https://github.com/robbiehanson/XMPPFramework

然后

在 - (无效)goOnline变化

#warning Set here Server Name... 
     if([domain isEqualToString:@"rakeshs-mac-mini.local"]) 
     { 
      NSXMLElement *priority = [NSXMLElement elementWithName:@"priority" stringValue:@"24"]; 
      [presence addChild:priority]; 
     } 

然后

在 - ( BOOL)连接方法..

#warning Set Username as [email protected] 
    myJID = [NSString stringWithFormat:@"%@@rakeshs-Mac-mini.local",myJID]; 

    [xmppStream setMyJID:[XMPPJID jidWithString:myJID]]; 
    password = myPassword; 

    NSLog(@"username: %@,Password : %@",myJID,myPassword); 

连接到你的服务器从Openfire的, 你能搞到响应从下面方法之后。

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message 

//尝试了上述测试的代码,如果有问题告诉我们..

+0

这是在做什么?入境登记?投票支持 – 2015-02-24 16:14:37

+0

@LaurStefan,首先在我的情况下,我已经在我的电脑中创建了** Openfire ** Server中的少数用户。然后,我通过Adium&** XMPP ** Demo与这两位用户登录后... 因此,它工作得很完美。我有**测试**它。 – Mehul 2015-02-25 06:26:10

0

//这种方法用于唱了起来视图控制器

-(BOOL)createNewAccountForXmppWithUserName:(NSString*)userNameJID andPassword:(NSString*)userPassword{ 
    if (userNameJID == nil || userPassword == nil) { 
     return NO; 
    } 
    NSString *domain = @"abc.com"; 
    self.xmppStream.hostName = domain; 
    int port = 5222; 
    self.xmppStream.hostPort = port; 
    useSSL    = NO; 
    customCertEvaluation = NO; 
    NSString * userName = [NSString stringWithFormat:@"%@@abc.com",userNameJID]; 
    XMPPJID *jid = [XMPPJID jidWithString:userName resource:nil]; 
    self.xmppStream.myJID = jid; 
    NSError *error = nil; 
    BOOL success; 
    success = [[self xmppStream] registerWithPassword:password error:&error]; 
    if(![[self xmppStream] isConnected]) 
    { 
     if (useSSL) 
      success = [[self xmppStream] oldSchoolSecureConnectWithTimeout:XMPPStreamTimeoutNone error:&error]; 
     else 
      success = [[self xmppStream] connectWithTimeout:XMPPStreamTimeoutNone error:&error]; 
     password = userPassword; 
     success = [[self xmppStream] registerWithPassword:password error:&error]; 
    } 
    else 
    { 
     password = userPassword; 
     success = [[self xmppStream] registerWithPassword:password error:&error]; 
    } 
    if (success) 
    { 
     isRegistering = YES; 
     NSLog(@"Successfully Register on XMPP Server"); 
    }  
    return YES; 

} 

要显示在线/离线状态,我们必须实现“NSFetchedResultsControllerDelegate”

@interface AKSMessageViewController : UIViewController<UITableViewDataSource,UITableViewDelegate, NSFetchedResultsControllerDelegate> 
{ 
    NSFetchedResultsController *fetchedResultsController; 
} 

并实行

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    //remove previous data or clear array 

    [[self xmppUserArray] removeAllObjects]; 
    [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] removeAllObjects]; 


    //get data from core data 
    self.xmppUserArray=[[[self fetchedResultsController] fetchedObjects] mutableCopy]; 


    for (int i=0; i<[[self xmppUserArray] count]; i++) { 

     if ([[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"sectionNum"] integerValue]==0) { 
      //this is user is online 
      [[[AKSGetCareerGlobalClass SharedInstance] onlineUserArray] addObject:[[[self xmppUserArray] objectAtIndex:i] valueForKey:@"nickname"]]; 

     } 
    } 


    [[self msgTableView] reloadData]; 

} 

//而

#pragma mark NSFetchedResultsController 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (fetchedResultsController == nil) 
    { 
     NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_roster]; 

     NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" 
                inManagedObjectContext:moc]; 

     NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES]; 
     NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES]; 

     NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil]; 
     //NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES]; 

     //NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userJID"]; 
     //NSLog(@"My JID ====>%@",myJID); 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subscription=='both'"]; 


     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     [fetchRequest setEntity:entity]; 
     [fetchRequest setPredicate:predicate]; 
     [fetchRequest setSortDescriptors:sortDescriptors]; 
     [fetchRequest setFetchBatchSize:20]; 

     fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                     managedObjectContext:moc 
                     sectionNameKeyPath:@"sectionNum" 
                        cacheName:nil]; 
     [fetchedResultsController setDelegate:self]; 


     NSError *error = nil; 
     if (![fetchedResultsController performFetch:&error]) 
     { 
      DDLogError(@"Error performing fetch: %@", error); 
     } 

    } 

    return fetchedResultsController; 
} 
+0

我使用上面的代码创建成功但没有显示为在线,并没有调用didAuthenticate方法,但是当关闭我的应用程序并重新启动,然后它显示为连接和online.where我失踪请帮助我 – Bittoo 2016-03-10 11:48:18

+0

@Bittoo。看到更新的代码。 “sectionNum”== 0表示在线用户。此方法“controllerDidChangeContent”将在任何用户上线/离线时调用。你不需要“didAuthenticate”方法。 – 2016-03-10 13:48:22

+0

感谢您的重播,但在我的情况下,我需要从手机中获取联系人,它也工作正常,但没有显示第一次当我关闭我的应用程序,然后重新打开,然后我拿它显示我的联系人,也调用didauthenticate方法。第一次只有得到问题,请帮助我 – Bittoo 2016-03-11 04:23:56

相关问题