当前位置:编程学习 > wap >>

[iPhone高级] 基于XMPP的IOS聊天客户端程序

为了方便程序调用,我们把XMPP的一些主要方法写在AppDelegate中

 \


在AppDelegate.m下这几个方法为:


[java]
-(void)setupStream{ 
     
    //初始化XMPPStream  
    xmppStream = [[XMPPStream alloc] init]; 
    [xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; 
     

 
-(void)goOnline{ 
     
    //发送在线状态  
    XMPPPresence *presence = [XMPPPresence presence]; 
    [[self xmppStream] sendElement:presence]; 
     

 
-(void)goOffline{ 
     
    //发送下线状态  
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"]; 
    [[self xmppStream] sendElement:presence]; 
     

 
-(BOOL)connect{ 
     
    [self setupStream]; 
     
    //从本地取得用户名,密码和服务器地址  
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     
    NSString *userId = [defaults stringForKey:USERID]; 
    NSString *pass = [defaults stringForKey:PASS]; 
    NSString *server = [defaults stringForKey:SERVER]; 
     
    if (![xmppStream isDisconnected]) { 
        return YES; 
    } 
     
    if (userId == nil || pass == nil) { 
        return NO; 
    } 
     
    //设置用户  
    [xmppStream setMyJID:[XMPPJID jidWithString:userId]]; 
    //设置服务器  
    [xmppStream setHostName:server]; 
    //密码  
    password = pass; 
     
    //连接服务器  
    NSError *error = nil; 
    if (![xmppStream connect:&error]) { 
        NSLog(@"cant connect %@", server); 
        return NO; 
    } 
     
    return YES; 
 

 
-(void)disconnect{ 
     
    [self goOffline]; 
    [xmppStream disconnect]; 
     

-(void)setupStream{
   
    //初始化XMPPStream
    xmppStream = [[XMPPStream alloc] init];
    [xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()];
   
}

-(void)goOnline{
   
    //发送在线状态
    XMPPPresence *presence = [XMPPPresence presence];
    [[self xmppStream] sendElement:presence];
   
}

-(void)goOffline{
   
    //发送下线状态
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
    [[self xmppStream] sendElement:presence];
   
}

-(BOOL)connect{
   
    [self setupStream];
   
    //从本地取得用户名,密码和服务器地址
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   
    NSString *userId = [defaults stringForKey:USERID];
    NSString *pass = [defaults stringForKey:PASS];
    NSString *server = [defaults stringForKey:SERVER];
   
    if (![xmppStream isDisconnected]) {
        return YES;
    }
   
    if (userId == nil || pass == nil) {
        return NO;
    }
   
    //设置用户
    [xmppStream setMyJID:[XMPPJID jidWithString:userId]];
    //设置服务器
    [xmppStream setHostName:server];
    //密码
    password = pass;
   
    //连接服务器
    NSError *error = nil;
    if (![xmppStream connect:&error]) {
        NSLog(@"cant connect %@", server);
        return NO;
    }
   
    return YES;

}

-(void)disconnect{
   
    [self goOffline];
    [xmppStream disconnect];
   
}这几个是基础方法,接下来就是XMPPStreamDelegate中的方法,也是接受好友状态,接受消息的重要方法


[java]
//连接服务器  
- (void)xmppStreamDidConnect:(XMPPStream *)sender{ 
     
    isOpen = YES; 
    NSError *error = nil; 
    //验证密码  
    [[self xmppStream] authenticateWithPassword:password error:&error]; 
     

 
//验证通过  
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{ 
     
    [self goOnline]; 

 
//收到消息  
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{ 
     
//    NSLog(@"message = %@", message);  
     
    NSString *msg = [[message elementForName:@"body"] stringValue]; 
    NSString *from = [[message attributeForName:@"from"] stringValue]; 
     
    NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
    [dict setObject:msg forKey:@"msg"]; 
    [dict setObject:from forKey:@"sender"]; 
     
    //消息委托(这个后面讲)  
    [messageDelegate newMessageReceived:dict]; 
     

 
//收到好友状态  
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence{ 
  &nbs

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,