#import "XMPPData.h" #import "XMPPFramework.h" //导入花名册相关头文件 #import "XMPPRoster.h" #import "XMPPRosterMemoryStorage.h" #import "XMPPRosterCoreDataStorage.h" @interface XMPPData()<XMPPStreamDelegate,XMPPRosterDelegate> @property(strong,nonatomic) XMPPStream *xmppStream; @property(assign,nonatomic) BOOL isRegister; @property(strong,nonatomic) NSString*user,*pwd,*hostName,*domain; @property(assign,nonatomic) UInt16 port; @property(strong,nonatomic) XMPPRosterCoreDataStorage * rosterStorage;//花名册存储 @property(strong,nonatomic) XMPPRoster * rosterModule;//花名册模块 @end @implementation XMPPData //开始连接聊天服务器 -(BOOL) connectWithUserName:(NSString*) username andPwd:(NSString*) pwd andHostName:(NSString*) hostname andDomain:(NSString*) domain andHostPort:(UInt16) port andIsRegister:(BOOL) isRegister { self.hostName = hostname; self.port = port; self.domain = domain; self.user = username; self.pwd = pwd; self.isRegister = isRegister; self.xmppStream = [[XMPPStream alloc] init]; //设置代理 [self.xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)]; //设置聊天服务器地址 self.xmppStream .hostName = hostname; //设置聊天服务器端口 默认是5222 self.xmppStream.hostPort = port; //设置Jid 就是用户名 XMPPJID *jid = [XMPPJID jidWithUser:username domain:domain resource:@"test"]; self.xmppStream.myJID = jid; NSError * error = nil; //验证连接 [self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]; if (error) { NSLog(@"连接失败:%@",error); return NO; } else { //注册所有的模块 [self activeModules]; return YES; } } //激活相关的模块 -(void) activeModules { //1.花名册存储对象 self.rosterStorage = [XMPPRosterCoreDataStorage sharedInstance]; //2.花名册模块 self.rosterModule = [[XMPPRoster alloc] initWithRosterStorage:self.rosterStorage]; //3.激活此模块 [self.rosterModule activate:self.xmppStream]; //4.添加roster代理 [self.rosterModule addDelegate:self delegateQueue:dispatch_get_main_queue()]; } //添加好友 -(BOOL) addFriend:(NSString*) friendName { XMPPJID * friendJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",friendName,self.domain]]; [self.rosterModule subscribePresenceToUser:friendJid]; return YES; } //删除好友 -(BOOL) deleteFriend:(NSString*) friendName { XMPPJID * friendJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",friendName,self.domain]]; [self.rosterModule removeUser:friendJid]; return YES; } //收到好友请求 代理函数 -(void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence { XMPPJID * fromJid = presence.from; //直接通过 [self.rosterModule acceptPresenceSubscriptionRequestFrom:fromJid andAddToRoster:YES]; } -(void)xmppRoster:(XMPPRoster *)sender didReceiveRosterItem:(DDXMLElement *)item { NSString *subscription = [item attributeStringValueForName:@"subscription"]; NSLog(@"%@",subscription); if ([subscription isEqualToString:@"both"]) { NSLog(@"双方成为好友!"); } } //连接成功的代理函数 -(void)xmppStreamDidConnect:(XMPPStream *)sender { NSLog(@"连接成功"); if (self.isRegister) { NSError* error = nil; [sender registerWithPassword:self.pwd error:&error]; if (error) { NSLog(@"注册失败1,%@",error); } } else { NSError *error = nil; [sender authenticateWithPassword:self.pwd error:&error]; if (error) { NSLog(@"验证失败1,%@",error); } } } //连接失败代理函数 -(void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error { NSLog(@"连接失败,%@",error); } //验证成功代理函数 -(void)xmppStreamDidAuthenticate:(XMPPStream *)sender { NSLog(@"验证成功"); //4.登陆成功后 在线 [self.xmppStream sendElement:[XMPPPresence presence]];//用户在线 }
原文地址:http://blog.csdn.net/fhongchan/article/details/46544035