首页 > 代码库 > XMPP iOS客户端实现三:登录、注册

XMPP iOS客户端实现三:登录、注册

1.创建一个单例模式来管理xmpp的连接和操作

 1 +(XMPPManager *)share
 2 {
 3     static XMPPManager *_share=nil;
 4     static dispatch_once_t onceToken;
 5     dispatch_once(&onceToken, ^{
 6         _share=[[XMPPManager alloc] init];
 7     });
 8     return _share;
 9 }
10 -(id)init
11 {
12     if (self=[super init]) {
13         [self setupStream];
14     }
15     return self;
16 }
17 - (void)setupStream
18 {
19      xmppStream = [[XMPPStream alloc] init];
20    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
21      [xmppStream setHostName:@"127.0.0.1"];
22      [xmppStream setHostPort:5222];
23 }

 2.登录和注册都需要使用用户名进行连接服务器,

-(void)login
{
    [self connect];
}
-(void)reg
{
    isReg=YES;
    [self connect];
}
- (BOOL)connect
{
    if (xmppStream.isConnected) {
        [self disconnect];
    }
    if (![xmppStream isDisconnected]) {
        return YES;
    }

    if (self.userName == nil) {
        return NO;
    }
    
    [xmppStream setMyJID:[XMPPJID jidWithString:self.userName]];

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

        return NO;
    }
    
    return YES;
}

3.在连接代理中判断是登录还是注册,发送不同命令

//成功连接服务器
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
//连接服务器失败
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

}
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

    NSError *error = nil;
    if (isReg)
    {
        if (![xmppStream registerWithPassword:self.pwd error:&error])
        {

        }
    }
    else
    {
        if (![xmppStream authenticateWithPassword:self.pwd error:&error])
        {
            

        }
    }
    
}

4.处理登录或注册代理函数

//登录成功
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    
    [self goOnline];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"登录成功"
                                                        message:@"登录成功"
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}
//登录失败
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"密码错误"
                                                        message:@"密码错误"
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
   

}
//注册成功
- (void)xmppStreamDidRegister:(XMPPStream *)sender
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册成功"
                                                        message:@"注册成功"
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
    isReg=NO;
}
//注册失败
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
    NSLog(@"%@",[[error elementForName:@"error"] stringValue]);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册失败"
                                                        message:@"注册失败"
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
     isReg=NO;
}

 

5.登录或注册的用户名必须“username@主机名”这种格式

转载请注明:http://www.cnblogs.com/bandy/p/3756492.html