登录、注册、添加好友、好友列表、聊天.
1.下载XMPPFramework、并将下列文件拖入工程中。
2.添加所需框架
3.添加Header Search Paths和Other Linker Flags
1 libxml includes require that the target Header Search Paths contain 2 // /usr/include/libxml2 3 // and Other Linker Flags contain 4 // -lxml2
4.祛除警告
CGBitmapContextCreate 在 ios7下变化
详情请访问博客
1 #if __ipHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_1 2 int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPRemultipliedLast; 3 #else int bitmapInfo = kCGImageAlphaPremultipliedLast; #endif
5.在spark注册账号,注册信息在openfire的ofUser里。
6.开始
1 // 2 // AppDelegate.h 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 #import "XmppManager.h" 11 @class XmppManager; 12 @interface AppDelegate : UIResponder <UIapplicationDelegate> 13 { 14 15 } 16 @property (strong, nonatomic) UIWindow *window; 17 @property (nonatomic,retain) XmppManager * xmppManager; 18 19 + (AppDelegate*)shareAppDelegate; 20 21 @endAppDelegate.h
1 // 2 // AppDelegate.m 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import "AppDelegate.h" 10 #import "RootViewController.h" 11 #import "LoginViewController.h" 12 #import "Define.h" 13 @implementation AppDelegate 14 15 16 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 17 { 18 19 //初始化xmpp管理类。 20 _xmppManager = [[XmppManager alloc] init]; 21 22 RootViewController * rootViewController = [[RootViewController alloc] init]; 23 UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 24 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 25 self.window.backgroundColor = [UIColor whiteColor]; 26 self.window.rootViewController = navigationController; 27 [rootViewController release]; 28 [navigationController release]; 29 [self.window makeKeyAndVisible]; 30 31 32 //如果未登录,拉起登录页面 33 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 34 NSDictionary * userInfo = [userDefaults objectForKey:USER_INFO_KEY]; 35 if (!userInfo) 36 { 37 LoginViewController * loginVC = [[LoginViewController alloc] init]; 38 UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC]; 39 [loginVC release]; 40 [navigationController presentViewController:loginNav animated:YES completion:^{ 41 }]; 42 } 43 44 45 return YES; 46 } 47 48 + (AppDelegate*)shareAppDelegate 49 { 50 return (AppDelegate*)[UIApplication sharedApplication].delegate; 51 } 52 53 - (void)applicationWillResignActive:(UIApplication *)application 54 { 55 56 } 57 58 - (void)applicationDidEnterBackground:(UIApplication *)application 59 { 60 [_xmppManager disconnect]; 61 } 62 63 - (void)applicationWillEnterForeground:(UIApplication *)application 64 { 65 66 } 67 68 - (void)applicationDidBecomeActive:(UIApplication *)application 69 { 70 [_xmppManager connect]; 71 72 } 73 74 - (void)applicationWillTerminate:(UIApplication *)application 75 { 76 [_xmppManager disconnect]; 77 } 78 79 - (void)dealloc 80 { 81 [_xmppManager release]; 82 [_window release]; 83 [super dealloc]; 84 } 85 86 @endAppDelegate.m
1 // 2 // XmppManager.h 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @class XMPPStream; 12 @interface XmppManager : NSObject 13 { 14 XMPPStream * xmppStream; 15 NSString * passWord; //密码 16 } 17 @property(nonatomic,readonly)XMPPStream * xmppStream; 18 @property(nonatomic,assign) id chatDelegate; 19 @property(nonatomic,assign) id statusDelegate; 20 21 22 //连接 23 - (BOOL)connect; 24 //断开 25 - (void)disconnect; 26 //设置XMPPStream 27 - (void)setupStream; 28 //上线 29 - (void)goOnline; 30 //下线 31 - (void)goOffline; 32 @end 33 34 35 @protocol ChatMessageDelegate <NSObject> 36 37 - (void)chatMessageDidReceived:(NSDictionary *)messageDic; 38 39 @end 40 41 @protocol FriendStautsDelegate <NSObject> 42 43 - (void)friendStatusDidOnLine:(NSDictionary *)statusDic; 44 - (void)friendStatusDidOffLine:(NSDictionary *)statusDic; 45 46 @endXmppManager.h
1 // 2 // XmppManager.m 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import "XmppManager.h" 10 #import "Define.h" 11 #import "XMPP.h" 12 13 @implementation XmppManager 14 15 //初始化XMPPStream 16 -(void)setupStream 17 { 18 xmppStream = [[XMPPStream alloc] init]; 19 [xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; 20 21 } 22 23 //发送在线状态 24 -(void)goOnline{ 25 26 XMPPPresence *presence = [XMPPPresence presence]; 27 [[self xmppStream] sendElement:presence]; 28 29 } 30 31 //发送下线状态 32 -(void)goOffline{ 33 34 XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"]; 35 [[self xmppStream] sendElement:presence]; 36 37 } 38 39 //连接 40 -(BOOL)connect{ 41 42 [self setupStream]; 43 44 //从本地取得用户名,密码和服务器地址 45 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 46 NSDictionary * userInfoDic = [defaults objectForKey:USER_INFO_KEY]; 47 NSString * userId = [userInfoDic objectForKey:NAME_KEY]; 48 NSString * pass = [userInfoDic objectForKey:PASSWORD_KEY]; 49 NSString * server = [userInfoDic objectForKey:SERVER_KEY]; 50 51 // 通常xmpp服务器的ip地址是公网Ip,通信的用户名就是 用户名@服务器地址 类似邮箱地址 52 // NSString * trueUserId = [NSString stringWithFormat:@"%@@%@",userId,server]; 53 54 // 因蓝鸥的xmpp服务器地址 是搭建在内网ip上,所以用户名的server 需要用“localhost”才能正常通信,不能使用内网ip作为sever地址 55 NSString * trueUserId = [NSString stringWithFormat:@"%@@%@",userId,@"localhost"]; 56 57 if (![xmppStream isDisconnected]) 58 { 59 return YES; 60 } 61 62 if (trueUserId == nil || pass == nil) 63 { 64 return NO; 65 } 66 67 //设置用户 68 [xmppStream setMyJID:[XMPPJID jidWithString:trueUserId]]; 69 //设置服务器 70 [xmppStream setHostName:server]; 71 //密码 72 password = pass; 73 74 //连接服务器 75 NSError *error = nil; 76 if (![xmppStream connect:&error]) 77 { 78 NSLog(@"cant connect %@", server); 79 return NO; 80 }else 81 { 82 NSLog(@"connect %@ successful", server); 83 } 84 85 return YES; 86 87 } 88 89 //断开连接 90 - (void)disconnect 91 { 92 93 [self goOffline]; 94 [xmppStream disconnect]; 95 96 } 97 98 //此方法在stream开始连接服务器的时候调用 99 - (void)xmppStreamDidConnect:(XMPPStream *)sender{ 100 101 NSError *error = nil; 102 //验证密码 103 [[self xmppStream] authenticateWithPassword:password error:&error]; 104 } 105 106 //验证成功后调用 107 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{ 108 109 //发送上线状态 110 [self goOnline]; 111 } 112 113 //验证失败后调用 114 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error 115 { 116 NSLog(@"auth failer"); 117 } 118 119 //收到消息 120 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{ 121 122 NSLog(@"xml message = %@", message); 123 124 NSString * msg = [[message elementForName:@"body"] stringValue]; 125 NSString * fromUser = [[message attributeForName:@"from"] stringValue]; 126 NSString * toUser = [[message attributeForName:@"to"] stringValue]; 127 //这个开源项目一个bug,有时键盘刚一输入时会收到空消息 128 if (!msg) { 129 NSLog(@"msg == nil!"); 130 return; 131 } 132 133 //封装一个消息字典,回传给使用类 134 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 135 [dict setObject:msg forKey:@"message"]; 136 [dict setObject:fromUser forKey:@"fromUserId"]; 137 [dict setObject:toUser forKey:@"toUserId"]; 138 139 NSLog(@"receive message = %@",msg); 140 NSLog(@"chat delegate = %@",_chatDelegate); 141 if (_chatDelegate && [_chatDelegate respondsToSelector:@selector(chatMessageDidReceived:)]) 142 { 143 [_chatDelegate chatMessageDidReceived:dict]; 144 } 145 146 } 147 148 //接受到好友状态更新 149 - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence 150 { 151 152 NSLog(@"presence = %@", presence); 153 154 //取得好友状态 155 NSString *presenceType = [presence type]; //online/offline 156 //当前用户 157 NSString *userId = [[sender myJID] user]; 158 //在线用户 159 NSString *presenceFromUser = [[presence from] user]; 160 161 162 NSLog(@"tyep = %@",presenceType); 163 if (![presenceFromUser isEqualToString:userId]) { 164 165 NSMutableDictionary * statusDic = [NSMutableDictionary dictionaryWithCapacity:1]; 166 167 //在线状态,用来把好友划分在在线列表还是离线列表 168 if ([presenceType isEqualToString:@"available"]) 169 { 170 [statusDic setObject:presenceFromUser forKey:@"name"]; 171 [statusDic setObject:@"available" forKey:@"status"]; 172 173 174 NSLog(@"status delegate = %@",_statusDelegate); 175 //通知delegate 上线 176 if(_statusDelegate && [_statusDelegate respondsToSelector:@selector(friendStatusDidOnLine:)]) 177 { 178 // NSLog(@"aaa"); 179 [_statusDelegate friendStatusDidOnLine:statusDic]; 180 } 181 182 183 }else if ([presenceType isEqualToString:@"unavailable"]) 184 { 185 186 [statusDic setObject:presenceFromUser forKey:@"name"]; 187 [statusDic setObject:@"unavailable" forKey:@"status"]; 188 189 //通知delegate 下线 190 if(_statusDelegate && [_statusDelegate respondsToSelector:@selector(friendStatusDidOnLine:)]) 191 { 192 [_statusDelegate friendStatusDidOffLine:statusDic]; 193 } 194 195 } 196 197 } 198 } 199 200 //重写getter方法 201 - (XMPPStream *)xmppStream 202 { 203 if (!xmppStream) 204 { 205 xmppStream = [[XMPPStream alloc] init]; 206 [xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()]; 207 } 208 return xmppStream; 209 } 210 211 212 @endXmppManager.m
1 // 2 // LoginViewController.h 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface LoginViewController : UIViewController 12 { 13 UITextField * _userName; 14 UITextField * _passWorld; 15 UITextField * _server; 16 17 } 18 @endLoginViewController.h
1 // 2 // LoginViewController.m 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import "LoginViewController.h" 10 #import "Define.h" 11 12 @interface LoginViewController () 13 14 @end 15 16 @implementation LoginViewController 17 18 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 23 CGFloat x = 80; 24 CGFloat y = 20; 25 CGFloat w = 160; 26 CGFloat h = 30; 27 CGRect rect = CGRectMake(x, y, w, h); 28 _userName = [[UITextField alloc] init]; 29 _userName.frame = rect; 30 _userName.borderStyle = UITextBorderStyleRoundedRect; 31 _userName.placeholder = @"user name"; 32 _userName.text = @""; 33 [self.view addSubview:_userName]; 34 35 x = x; 36 y = y + h + 30; 37 w = w; 38 h = h; 39 rect = CGRectMake(x, y, w, h); 40 _passWorld = [[UITextField alloc] init]; 41 _passWorld.frame = rect; 42 _passWorld.borderStyle = UITextBorderStyleRoundedRect; 43 _passWorld.placeholder = @"password"; 44 _passWorld.text = @""; 45 [self.view addSubview:_passWorld]; 46 47 x = x; 48 y = y + h + 30; 49 w = w; 50 h = h; 51 rect = CGRectMake(x, y, w, h); 52 _server = [[UITextField alloc] init]; 53 _server.frame = rect; 54 _server.borderStyle = UITextBorderStyleRoundedRect; 55 _server.placeholder = @"server"; 56 _server.text = @""; 57 [self.view addSubview:_server]; 58 59 60 UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"login" 61 style:UIBarButtonItemStyleBordered 62 target:self 63 action:@selector(login)]; 64 self.navigationItem.rightBarButtonItem = barButtonItem; 65 [barButtonItem release]; 66 67 } 68 69 - (void)login 70 { 71 if ([_userName.text isEqualToString:@""] || 72 [_passWorld.text isEqualToString:@""] || 73 [_passWorld.text isEqualToString:@""]) 74 { 75 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" 76 message:@"信息不完整!" 77 delegate:self 78 cancelButtonTitle:@"确定" 79 otherButtonTitles:nil, nil]; 80 [alertView show]; 81 [alertView release]; 82 83 return; 84 } 85 86 NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: 87 _userName.text,NAME_KEY, 88 _passWorld.text,PASSWORD_KEY, 89 _server.text,SERVER_KEY, nil]; 90 91 //登录信息存入本地 92 NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults]; 93 [userDefault setObject:dic forKey:USER_INFO_KEY]; 94 95 [self dismissViewControllerAnimated:YES completion:^{ 96 97 }]; 98 } 99 100 101 - (void)dealloc 102 { 103 [_userName release]; 104 [_passWorld release]; 105 [_server release]; 106 [super dealloc]; 107 } 108 109 110 111 @endLoginViewController.m
1 // 2 // RootViewController.h 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface RootViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> 12 { 13 14 } 15 @property (nonatomic,retain)UITableView * tableView; 16 @property (nonatomic,retain)NSMutableArray * dataArray; 17 @endRootViewController.h
1 // 2 // RootViewController.m 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import "RootViewController.h" 10 #import "ChatViewController.h" 11 12 #import "AppDelegate.h" 13 #import "Define.h" 14 15 @interface RootViewController () 16 17 @end 18 19 @implementation RootViewController 20 21 - (void)viewDidLoad 22 { 23 [super viewDidLoad]; 24 25 self.navigationItem.title = @"好友列表"; 26 27 //好友listview 28 _tableView = [[UITableView alloc] initWithFrame: 29 CGRectMake(0, 0, 320, self.view.bounds.size.height) 30 style:UITableViewStylePlain]; 31 _tableView.delegate = self; 32 _tableView.dataSource = self; 33 [self.view addSubview:_tableView]; 34 35 self.dataArray = [NSMutableArray array]; 36 37 38 //设置好友状态的delegate 39 [AppDelegate shareAppDelegate].xmppManager.statusDelegate = self; 40 } 41 42 - (void)viewWillAppear:(BOOL)animated 43 { 44 [super viewWillAppear:animated]; 45 46 //如果已经登录过,让xmpp 连接 47 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 48 NSDictionary * userInfo = [defaults objectForKey:USER_INFO_KEY]; 49 if (userInfo) 50 { 51 [[AppDelegate shareAppDelegate].xmppManager connect]; 52 } 53 54 } 55 56 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 57 { 58 return _dataArray.count; 59 } 60 61 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 62 { 63 64 static NSString * cellIndentifier = @"cell"; 65 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier]; 66 if (!cell) 67 { 68 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier] autorelease]; 69 } 70 71 NSString * userName = [self.dataArray objectAtIndex:indexPath.row]; 72 73 cell.textLabel.text = userName; 74 return cell; 75 } 76 77 //选择和某人聊天 78 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 79 { 80 NSString * name = [self.dataArray objectAtIndex:indexPath.row]; 81 ChatViewController * chatViewController = [[ChatViewController alloc] initWithUserName:name]; 82 [self.navigationController pushViewController:chatViewController animated:YES]; 83 [chatViewController release]; 84 } 85 86 #pragma Friend status delegate 87 //收到好友上线状态 88 - (void)friendStatusDidOnLine:(NSDictionary *)statusDic 89 { 90 NSString * name = [statusDic objectForKey:@"name"]; 91 if (![_dataArray containsObject:name]) { 92 [_dataArray addObject:name]; 93 [_tableView reloadData]; 94 } 95 } 96 97 //收到好友下线状态 98 - (void)friendStatusDidOffLine:(NSDictionary *)statusDic 99 { 100 NSString * name = [statusDic objectForKey:@"name"]; 101 [_dataArray removeObject:name]; 102 [_tableView reloadData]; 103 } 104 105 106 107 - (void)dealloc 108 { 109 [_tableView release]; 110 [super dealloc]; 111 } 112 @endRootViewController.m
1 // 2 // ChatViewController.h 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface ChatViewController : UIViewController<UIScrollViewDelegate> 12 { 13 14 } 15 16 @property (nonatomic,copy)NSString * chatUserName; 17 @property (nonatomic,retain)UITextField * chatInputTextField; 18 @property (nonatomic,retain)UILabel * chatReceiveLabel; 19 @property (nonatomic,retain)UILabel * chatSendLabel; 20 21 - (id)initWithUserName:(NSString *)userName; 22 23 24 @endChatViewController.h
1 // 2 // ChatViewController.m 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 #import "ChatViewController.h" 10 #import "XMPP.h" 11 #import "Define.h" 12 #import "AppDelegate.h" 13 14 @interface ChatViewController () 15 16 @end 17 18 @implementation ChatViewController 19 20 //重新初始化方法,确定和谁聊天 21 - (id)initWithUserName:(NSString *)userName 22 { 23 self = [super init]; 24 if (self) 25 { 26 self.chatUserName = userName; 27 28 //设置收到消息后的delegate 29 [AppDelegate shareAppDelegate].xmppManager.chatDelegate = self; 30 } 31 return self; 32 } 33 34 35 - (void)viewDidLoad 36 { 37 38 [super viewDidLoad]; 39 40 self.navigationItem.title = [NSString stringWithFormat:@"与%@聊天中",_chatUserName]; 41 42 //输入框。 43 self.chatInputTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 200, 30)]; 44 _chatInputTextField.borderStyle = UITextBorderStyleRoundedRect; 45 [self.view addSubview:_chatInputTextField]; 46 [_chatInputTextField release]; 47 48 //发送button 49 UIButton * sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 50 [sendButton setFrame:CGRectMake(_chatInputTextField.frame.origin.x 51 + _chatInputTextField.frame.size.width + 10, 52 _chatInputTextField.frame.origin.y, 60, 30)]; 53 [sendButton setTitle:@"发送" forState:UIControlStateNormal]; 54 [sendButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside]; 55 [self.view addSubview:sendButton]; 56 57 //显示收到的文本label 58 self.chatReceiveLabel = [[UILabel alloc] initWithFrame: 59 CGRectMake(_chatInputTextField.frame.origin.x, 60 _chatInputTextField.frame.origin.y + 61 _chatInputTextField.frame.size.height + 20, 270, 100)]; 62 _chatReceiveLabel.numberOfLines = 0; 63 _chatReceiveLabel.lineBreakMode = NSLineBreakByWordWrapping; 64 _chatReceiveLabel.backgroundColor = [UIColor grayColor]; 65 [self.view addSubview:_chatReceiveLabel]; 66 [_chatReceiveLabel release]; 67 68 69 //显示发送的文本label 70 self.chatSendLabel = [[UILabel alloc] initWithFrame:CGRectMake(_chatReceiveLabel.frame.origin.x, _chatReceiveLabel.frame.origin.y + _chatReceiveLabel.frame.size.height+ 10, 270, 100)]; 71 _chatSendLabel.numberOfLines = 0; 72 _chatSendLabel.lineBreakMode = NSLineBreakByWordWrapping; 73 _chatSendLabel.backgroundColor = [UIColor grayColor]; 74 [self.view addSubview:_chatSendLabel]; 75 [_chatSendLabel release]; 76 77 78 } 79 80 //组装发消息的xml内容 81 - (void)sendMessage:(id)sender 82 { 83 //本地输入框中的信息 84 NSString *message = self.chatInputTextField.text; 85 86 if (message.length > 0) { 87 88 89 //XMPPFramework主要是通过KissXML来生成XML文件 90 //生成<body>文档 91 NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; 92 [body setStringValue:message]; 93 94 //生成XML消息文档 95 NSXMLElement *mes = [NSXMLElement elementWithName:@"message"]; 96 //消息类型 97 [mes addAttributeWithName:@"type" stringValue:@"chat"]; 98 //发送给谁 99 NSString * toUser = [NSString stringWithFormat:@"%@@%@",_chatUserName,@"localhost"]; 100 101 [mes addAttributeWithName:@"to" stringValue:toUser]; 102 //由谁发送 103 NSDictionary * userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:USER_INFO_KEY]; 104 NSString * formUser = [NSString stringWithFormat:@"%@@%@",[userInfo objectForKey:NAME_KEY],@"localhost"]; 105 [mes addAttributeWithName:@"from" stringValue:formUser]; 106 //组合 107 [mes addChild:body]; 108 109 //发送消息 110 [[self xmppStream] sendElement:mes]; 111 112 113 self.chatInputTextField.text = @""; 114 self.chatSendLabel.text = [NSString stringWithFormat:@"发送了:%@",message]; 115 [self.chatInputTextField resignFirstResponder]; 116 117 } 118 119 } 120 121 122 - (void)viewWillDisappear:(BOOL)animated 123 { 124 125 [super viewWillDisappear:animated]; 126 127 //delegate 赋空 128 [AppDelegate shareAppDelegate].xmppManager.chatDelegate = nil; 129 130 } 131 132 //收到消息后的代理回调。 133 - (void)chatMessageDidReceived:(NSDictionary *)messageDic 134 { 135 NSString * message = [messageDic objectForKey:@"message"]; 136 self.chatReceiveLabel.text = [NSString stringWithFormat:@"收到了:%@",message]; 137 } 138 139 //获取xmppstream 140 - (XMPPStream *)xmppStream 141 { 142 return [[AppDelegate shareAppDelegate].xmppManager xmppStream]; 143 } 144 145 - (void)dealloc 146 { 147 self.chatUserName = nil; 148 [super dealloc]; 149 } 150 151 @endChatViewController.m
1 // 2 // Define.h 3 // MyXmpp 4 // 5 // Created by ChenJungang on 14-1-3. 6 // Copyright (c) 2014年 chenjungang. All rights reserved. 7 // 8 9 10 #define USER_INFO_KEY @"userInfo" 11 12 13 #define NAME_KEY @"account" 14 #define PASSWORD_KEY @"password" 15 #define SERVER_KEY @"server"Define.h