首先下载xmppframework这个框架,下载
点ZIP下载
接下来,用Xcode新建一个工程
将以下这些文件拖入新建工程中
加入framework
并设置
为了方便程序调用,我们把XMPP的一些主要方法写在AppDelegate中
涉及三个文件,一个AppDelegate.m,一个好友controller,一个聊天controller
和android一样,聊天记录的持久化需要自己操作本地数据库
在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];
- }
- //连接服务器
- - (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"]];
- }
- }
- }
定义完两个委托,我们就要在不同的Controller中实现这两个委托了
在好友Controller中实现<KKChatDelegate>并写入如下方法
- //取得当前程序的委托
- -(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];
- }
- //设定在线用户委托
- 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];
- }
这里最重要的就是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;
- }
- -(KKAppDelegate *)appDelegate{
- return (KKAppDelegate *)[[UIApplication sharedApplication] delegate];
- }
- -(XMPPStream *)xmppStream{
- return [[self appDelegate] xmppStream];
- }
- 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文件
- //生成<body>文档
- 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];
- }
- }