接上一章的,这一章我们着重介绍XMPP
为了方便程序调用,我们把XMPP的一些主要方法写在AppDelegate中
在AppDelegate.m下这几个方法为:
-(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中的方法,也是接受好友状态,接受消息的重要方法
//连接服务器
- (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{
// NSLog(@"presence = %@", presence);
//取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user];
if (![presenceFromUser isEqualToString:userId]) {
//在线状态
if ([presenceType isEqualToString:@"available"]) {
//用户列表委托(后面讲)
[chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"nqc1338a"]];
}else if ([presenceType isEqualToString:@"unavailable"]) {
//用户列表委托(后面讲)
[chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"nqc1338a"]];
}
}
}
这里面有两个委托方法,一个是用户列表委托,还有一个就是消息委托,用户列表委托主要就是取得在线用户,更新用户TableView,消息委托就是取得好友发送的消息,并更新消息TableView,当然这两个TableView是在不同的Controller中的
定义完两个委托,我们就要在不同的Controller中实现这两个委托了
在好友Controller中实现
//取得当前程序的委托
-(KKAppDelegate *)appDelegate{
return (KKAppDelegate *)[[UIApplication sharedApplication] delegate];
}
//取得当前的XMPPStream
-(XMPPStream *)xmppStream{
return [[self appDelegate] xmppStream];
}
//在线好友
-(void)newBuddyOnline:(NSString *)buddyName{
if (![onlineUsers containsObject:buddyName]) {
[onlineUsers addObject:buddyName];
[self.tView reloadData];
}
}
//好友下线
-(void)buddyWentOffline:(NSString *)buddyName{
[onlineUsers removeObject:buddyName];
[self.tView reloadData];
}
在viewDidLoad中加入
//设定在线用户委托
KKAppDelegate *del = [self appDelegate];
del.chatDelegate = self;
这两行代码,让好友列表的委托实现方法在本程序中
在viewWillAppear中加入
[super viewWillAppear:animated];
NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
if (login) {
if ([[self appDelegate] connect]) {
NSLog(@"show buddy list");
}
}else {
//设定用户
[self Account:self];
}
判断本地保存的数据中是否有userId,没有的话就跳转到登录页面
这里最重要的就是connect了,这一句话就是登录了,成功的话,页面就会显示好友列表了。
#pragma mark UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//start a Chat
chatUserName = (NSString *)[onlineUsers objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:@"chat" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"chat"]) {
KKChatController *chatController = segue.destinationViewController;
chatController.chatWithUser = chatUserName;
}
}
当显示出好友列表,我们选择一个好友进行聊天
将当前好友名称发送给聊天页面
下面是聊天Controller了
在KKChatController.h中加入
NSMutableArray *messages;
这是我们要显示的消息,每一条消息为一条字典
接下来就是每一条消息的显示了
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"msgCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
NSMutableDictionary *dict = [messages objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:@"msg"];
cell.detailTextLabel.text = [dict objectForKey:@"sender"];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
跟上面好友Controller一样,这里我们也需要XMPPStream
-(KKAppDelegate *)appDelegate{
return (KKAppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(XMPPStream *)xmppStream{
return [[self appDelegate] xmppStream];
}
在ViewDidLoad中加入
KKAppDelegate *del = [self appDelegate];
del.messageDelegate = self;
设定消息委托由自己来接收和处理
#pragma mark KKMessageDelegate
-(void)newMessageReceived:(NSDictionary *)messageCotent{
[messages addObject:messageCotent];
[self.tView reloadData];
}
接下来最重要的就是发送消息了
- (IBAction)sendButton:(id)sender {
//本地输入框中的信息
NSString *message = self.messageTextField.text;
if (message.length > 0) {
//XMPPFramework主要是通过KissXML来生成XML文件
//生成文档
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:chatWithUser];
//由谁发送
[mes addAttributeWithName:@"from" stringValue:[[NSUserDefaults standardUserDefaults] stringForKey:USERID]];
//组合
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
self.messageTextField.text = @"";
[self.messageTextField resignFirstResponder];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:message forKey:@"msg"];
[dictionary setObject:@"you" forKey:@"sender"];
[messages addObject:dictionary];
//重新刷新tableView
[self.tView reloadData];
}
}
上面都加了注释,大家应该能明白,接下来还有一个章节,我们会对发送的消息在界面进行美化,跟苹果自带的消息一样。谢谢大家有耐心看完,我这个人比较不喜欢打字,所以有的地方注释比较少,希望大家别介意,还有希望大家能够多多支持, 以后会接着介绍XMPP文件传输之类的内容。