ios 即时通讯-xmpp相关代码如下:
#import <Foundation/Foundation.h>
#import "XMPPFramework.h"
#import "CommunViewController.h"
@protocol XMPPManagerDelegate <NSObject>
@required
-(void)receiveMessage:(XMPPMessage *)message;
@end
@interface TRXMPPManager : NSObject
+ (TRXMPPManager *)shareManager;
@property (nonatomic, strong)XMPPStream *xmppStream;
@property (nonatomic, strong)XMPPRoster *roster;
@property (nonatomic, weak)CommunViewController *friendsDelegate;
@property (nonatomic, weak)id<XMPPManagerDelegate> delegate;
@property (nonatomic, copy)NSString *password;
-(void)initXMPPWithUserName:(NSString *)name andPW:(NSString *)password;
-(XMPPMessage *)sendMessageToJidName:(NSString *)name withBody:(NSString *)body andType:(NSString *)type;
//获得好友列表
- (void)queryFriends;
//添加好友
-(void)addFriendByName:(NSString *)name;
//删除好友
- (void)deleteFriendByName:(NSString *)name;
@end
#import "TRXMPPManager.h"
static TRXMPPManager *_manager;
@implementation TRXMPPManager
+(TRXMPPManager *)shareManager{
if (!_manager) {
_manager = [[TRXMPPManager alloc]init];
}
return _manager;
}
-(void)initXMPPWithUserName:(NSString *)name andPW:(NSString *)password{
self.password = password;
self.xmppStream = [[XMPPStream alloc]init];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//设置服务器地址
[self.xmppStream setHostName:@"192.168.1.201"];
//设置端口
[self.xmppStream setHostPort:5222];
//设置当前用户
XMPPJID *myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@teamtek",name]];
[self.xmppStream setMyJID:myJID];
// 连接服务器
[self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:nil];
}
-(void)xmppStreamDidConnect:(XMPPStream *)sender{
NSLog(@"连接成功");
// 登陆 认证
[self.xmppStream authenticateWithPassword:self.password error:nil];
}
-(void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error{
NSLog(@"断开");
}
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
NSLog(@"登陆成功");
//通知服务器 登陆状态
[self.xmppStream sendElement:[XMPPPresence presence]];
}
//接收到消息
-(void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSLog(@"type = %@ body =%@ from = %@",message.type,message.body,message.fromStr);
//把接收到的message传递出去
[self.delegate receiveMessage:message];
}
-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error{
NSLog(@"登陆失败");
// 注册
[self.xmppStream registerWithPassword:self.password error:nil];
}
-(void)xmppStreamDidRegister:(XMPPStream *)sender{
NSLog(@"注册成功!");
[self.xmppStream authenticateWithPassword:self.password error:nil];
}
-(void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error{
NSLog(@"注册失败 %@",error);
}
-(XMPPMessage *)sendMessageToJidName:(NSString *)name withBody:(NSString *)body andType:(NSString *)type{
XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@teamtek",name]]];
[message addBody:body];
[self.xmppStream sendElement:message];
return message;
}
//发出获得好友列表请求
- (void)queryFriends {
NSXMLElement *query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"get"];
[iq addChild:query];
[self.xmppStream sendElement:iq];
}
//接收到服务器返回的好友列表时调用此方法
-(BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
if ([@"result" isEqualToString:iq.type]) {
NSXMLElement *query = iq.childElement;
if ([@"query" isEqualToString:query.name]) {
NSArray *items = [query children];
for (NSXMLElement *item in items) {
NSString *name = [item attributeStringValueForName:@"jid"];
NSLog(@"name--------%@",name);
//把得到的好友名称显示到列表页面
[self.friendsDelegate addFriend:name];
}
}
}
return YES;
}
-(XMPPRoster *)roster{
if (!_roster) {
XMPPRosterCoreDataStorage *xmppRosterDataStorage = [[XMPPRosterCoreDataStorage alloc] init];
_roster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterDataStorage];
[_roster activate:self.xmppStream];
}
return _roster;
}
//添加好友
-(void)addFriendByName:(NSString *)name{
//添加好友
XMPPJID *frendJID = [XMPPJID jidWithString :[NSString stringWithFormat:@"%@@teamtek",name]];
[self.roster addUser:frendJID withNickname:nil];
}
-(void)deleteFriendByName:(NSString *)name{
//删除好友
XMPPJID *friendJID = [XMPPJID jidWithString :name];
[self.roster removeUser:friendJID];
}
@end
聊天气泡
//聊天气泡
- (UIView *)bubbleView:(NSString *)text withName:(NSString *)imageName{
UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero]; returnView.backgroundColor = [UIColor clearColor];
if ([imageName isEqualToString:@"bubble-default-incoming-green.png"])
{
UIImage *bubble = [UIImage imageNamed:imageName];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:40 topCapHeight:25]];
UIFont *font = [UIFont systemFontOfSize:28];
CGSize size = [text boundingRectWithSize:CGSizeMake(150.f, 999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font}context:nil].size;
NSLog(@"font zize %f,%f",size.width,size.height);
UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 14.0f, size.width, size.height)];
bubbleText.backgroundColor = [UIColor clearColor];
bubbleText.font = font;
bubbleText.numberOfLines = 0;
//以字符为显示单位显示
bubbleText.lineBreakMode = NSLineBreakByCharWrapping;
bubbleText.text = text;
bubbleImageView.frame = CGRectMake(0, 0, 200.0f, size.height+30.0f);
returnView.frame = CGRectMake(10.0f, 10.0f+self.viewHight, 200.0f, size.height+50.0f);
self.viewHight += size.height + SPACE;
[returnView addSubview:bubbleImageView];
[returnView addSubview:bubbleText];
}
else
{
UIImage *bubble = [UIImage imageNamed:imageName];
UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:28 topCapHeight:14]];
UIFont *font = [UIFont systemFontOfSize:28];
CGSize size = [text boundingRectWithSize:CGSizeMake(150.f, 999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font}context:nil].size;
NSLog(@"font zize %f,%f",size.width,size.height);
UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width, size.height)];
bubbleText.backgroundColor = [UIColor clearColor];
bubbleText.font = font;
bubbleText.numberOfLines = 0;
//以字符为显示单位显示,后面部分省略不显示
bubbleText.lineBreakMode = NSLineBreakByCharWrapping;
bubbleText.text = text;
bubbleImageView.frame = CGRectMake(0, 0, 200.0f, size.height+30.0f);
returnView.frame = CGRectMake(450.0f, 10.0f+self.viewHight, 200.0f, size.height+50.0f);
self.viewHight += size.height + SPACE;
[returnView addSubview:bubbleImageView];
[returnView addSubview:bubbleText];
}
if (self.viewHight > BOUND_HEIGHT - 200) {
self.scrollView.contentSize = CGSizeMake(BOUND_WIDTH/3*2, self.viewHight+50);
[self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentOffset.y+250) animated:NO];
}
return returnView ;
}
UIView *view = [self bubbleView:message.body withName:@"bubble-default-incoming-green.png"];