这里只是列出实现的只要步骤,不是全部代码。

首先导入XMPPFramework,及相关配置,完成后开始。

创建一个XMPPHelper  类来管理要进行的操作。

XMPPHelper.h文件如下

 typedef void(^loginSuccessBlock)(void);
typedef void(^loginFailBlock)(NSString *info);
typedef void(^fenchFridendList)(id result);
typedef void(^logoutSuccessBlock)(void);
typedef void(^registerSuccessBlock)(void);
typedef enum {
kTypeLogin,
kTypeRegister
}ConnectType;
#import <Foundation/Foundation.h>
#define kXMPPHosetName @"lala"
#define kXMPPHostAddress @"192.168.13.17"
#define kHostPort 5222 @interface XMPPHelper : NSObject
{ ConnectType _connectType; }
@property(nonatomic,strong)XMPPStream *stream;
@property(nonatomic,strong)XMPPRoster *roster;
@property(nonatomic,copy)NSString *username;
@property(nonatomic,copy)NSString *password; @property(nonatomic,copy)loginSuccessBlock loginSuccessBlock;
@property(nonatomic,copy)loginFailBlock loginFailBlock;
@property(nonatomic,copy)fenchFridendList fenchFriendList;
@property(nonatomic,copy)logoutSuccessBlock logoutSuccessBlock;
@property(nonatomic,copy)registerSuccessBlock registerSuccessblock; +(instancetype)shareInstance;
//登陆
-(void)login:(NSString *)username password:(NSString *)password loginSuccessBlock:(loginSuccessBlock)block loginFailBlock:(loginFailBlock)failBlcok;
//获取好友列表
-(void)loadFriendList:(fenchFridendList)fenchfriendList;
//发送消息
-(void)sendMessage:(NSString *)message toUser:(NSString *)jid;
//注销
-(void)logoutAction:(logoutSuccessBlock)block;
//注册
-(void)registerAction:(NSString *)username password:(NSString *)password registerSuccessBlock:(registerSuccessBlock)registerBlock; @end

XMPPHelper.m文件如下

#define kHostAddress @"192.168.13.12"
#define kHostName @"zy"
#define KHostPort 5222
#import "XMPPHelper.h"
#import "User.h" static XMPPHelper *helper = nil;
@interface XMPPHelper() <XMPPStreamDelegate>{
ConnectTyep _connectType;
}
@end
@implementation XMPPHelper //单例
+ (instancetype) shareInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[XMPPHelper alloc] init];
[helper _stepXMPPHelper];
});
return helper;
} //初始化的操作
- (void) _stepXMPPHelper {
//初始化流对象.
self.stream = [[XMPPStream alloc] init];
//创建自动连接的对象,当意外断开的时候,可以进行自动连接.
XMPPReconnect *reConnect = [[XMPPReconnect alloc] init]; //初始化花名册对象(好友列表)
//存储方式.
XMPPRosterCoreDataStorage *storage = [[XMPPRosterCoreDataStorage alloc] init];
self.roster = [[XMPPRoster alloc] initWithRosterStorage:storage]; //建立对象之间的联系
//激活.
[reConnect activate:self.stream];
//激活,使用花名册对象.
[self.roster activate:self.stream]; //设置代理;
[self.stream addDelegate:self delegateQueue:dispatch_get_main_queue()];
[self.roster addDelegate:self delegateQueue:dispatch_get_main_queue()]; //设置服务器的基本信息.
//服务器的地址
[self.stream setHostName:kHostAddress];
[self.stream setHostPort:KHostPort];
//端口号
} #pragma mark XMPP 对外公开方法
- (void) login:(NSString *)userName password:(NSString *)password loginSuccessBlock:(LoginSuccessBlock)block loginFailBlock:(LoginFailBlock)failBlock { _connectType = kTypeLogin; self.userName = userName;
self.password = password;
self.loginFailBlock = failBlock;
self.loginSuccessBlock = block;
//登录的流程.
if (![self connect]) {
//连接失败的时候 调用失败的block
if (self.loginFailBlock) {
self.loginFailBlock(@"网络错误");
}
}
}
//发起连接
- (BOOL) connect {
//设置用户的jid
NSString *jidString = [NSString stringWithFormat:@"%@@%@",self.userName,kHostName];
//创建了一个jid对象
XMPPJID *jid = [XMPPJID jidWithString:jidString];
[self.stream setMyJID:jid];
NSError *ERROR = nil;
return [self.stream connectWithTimeout: error:&ERROR];
}
//上线的方法
- (void) online {
//默认情况下 上线的状态
XMPPPresence *presence = [XMPPPresence presence];
[self.stream sendElement:presence];
} //下线的方法
- (void) outLine { XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];//unavailable 离线的意思
//发送离线状态
[self.stream sendElement:presence]; } //查询好友列表 /**
<iq from='guojing@wxhl' type='get' id='roster_1' to='域名'>
<query xmlns='jabber:iq:roster'/> <!-- iq 信息有多种,根据命名空间来区分-->
</iq> */
- (void) loadFriendList :(FentchFriendList) fentchFriendList { self.fentchFriendList = fentchFriendList;
//创建xml片段
//创建<iq></iq>
NSXMLElement *iqElement = [NSXMLElement elementWithName:@"iq"];
XMPPJID *jid = self.stream.myJID;
[iqElement addAttributeWithName:@"from" stringValue:[jid description]];
//jid.domain ---> 服务器的域名
[iqElement addAttributeWithName:@"to" stringValue:jid.domain];
[iqElement addAttributeWithName:@"type" stringValue:@"get"];
[iqElement addAttributeWithName:@"id" stringValue:@""]; //创建子节点
NSXMLElement *queryElement = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
//给iq节点增加子节点.
[iqElement addChild:queryElement];
//发送请求(好友列表)
[self.stream sendElement:iqElement];
}
//发送消息
//发送消息的格式
/**
<message
to='huangrong@wxhl'
from='guojing@wxhl'
type='chat'
xml:lang='en'>
<body>有个bug帮我看下</body>
</message>
*/
- (void) sendMessage:(NSString *)message toUser:(NSString *) jid { //NIL
if (!message || [message isEqualToString:@""]) {
return;
} NSXMLElement *messageElement = [NSXMLElement elementWithName:@"message"]; [messageElement addAttributeWithName:@"to" stringValue:jid];
XMPPJID *myJid = self.stream.myJID;
[messageElement addAttributeWithName:@"from" stringValue:myJid.description]; [messageElement addAttributeWithName:@"type" stringValue:@"chat"]; [messageElement addAttributeWithName:@"xml:lang" stringValue:@"en"];
//<message></message>创建完毕
//创建子节点<body></body>
NSXMLElement *bodyElement = [NSXMLElement elementWithName:@"body" stringValue:message];
[messageElement addChild:bodyElement]; //发送消息
[self.stream sendElement:messageElement];
} //注销方法
- (void) logOutAction : (LogoutSuccessBlock) block {
//发送离线的消息
[self outLine]; //断开连接,主动断开链接
[self.stream disconnect]; if (block) {
block();
}
}
//注册的方法
- (void) registerUser:(NSString *)userName password:(NSString *)password registerSuccessBlock:(RegisterSuccessBlock) block {
_connectType = kTypeRegister;
//连接
self.userName = userName;
self.password = password;
self.registerBlock = block; [self connect];
} #pragma mark - XMPP DELEGAET
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
NSLog(@"连接成功");
//注册流程和登录流程都要使用该方法.在这里就要进行逻辑上判断
if (_connectType == kTypeRegister) {
[self.stream registerWithPassword:self.password error:nil];
}else if(_connectType == kTypeLogin){
//如果连接成功,则继续验证密码.
[self.stream authenticateWithPassword:self.password error:nil];
}
}
//验证成功的方法
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
NSLog(@"验证成功");
//要调用成功的block
//发送上线的状态.
[self online];
if (self.loginSuccessBlock) {
self.loginSuccessBlock();
}
}
//验证失败的方法
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error {
NSLog(@"验证失败");
//如果认证失败的话,要主动断开连接
[self.stream disconnect];
if (self.loginFailBlock) {
self.loginFailBlock(@"密码错误");
}
}
//获取查询信息
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
/**
//根节点
<iq xmlns="jabber:client" type="result" id="123456" to="1234@zy/f54fcd02">
<query xmlns="jabber:iq:roster">
<item jid="gxb@localhost" name="" ask="subscribe" subscription="none">
<group>联系人列表</group>
</item>
<item jid="laoyan@zy" name="laoyan" subscription="both">
<group>12</group>
</item>
<item jid="deskmate@zy" name="deskmate" subscription="both">
<group>12</group>
<group>12</group>
</item>
<item jid="test123@zy" name="test123" subscription="both">
<group>联系人列表</group>
</item>
<item jid="zhengqingqing@192.168.13.12" name="" ask="subscribe" subscription="none">
<group>联系人列表</group>
</item>
<item jid="liuweiwei@zy" name="" subscription="both">
<group>让你变绿</group>
<group>让你变绿</group>
</item>
<item jid="zhengqingqing@zy" name="zhengqingqing" subscription="both">
<group>让你变绿</group>
</item>
<item jid="gxb1@zy" name="" subscription="both">
<group>联系人列表</group>
<group>联系人列表</group>
<group>变绿了</group>
</item>
<item jid="123456@localhost" name="" ask="subscribe" subscription="none">
<group>联系人列表</group>
</item>
</query>
</iq>
*/
//需要要类型是 字典
/**
dic = {
"联系人列表":[user1,user3],
"12":[user2]
}
*/
NSMutableDictionary *results = [NSMutableDictionary dictionary];
NSXMLElement *queryElement = iq.childElement;
//遍历Query节点
User *user = nil;
for (NSXMLElement *itemElement in queryElement.children) { user = [[User alloc] init]; NSString *jidString = [itemElement attributeStringValueForName:@"jid"]; user.jid = jidString; NSString *userName = [itemElement attributeStringValueForName:@"name"]; user.username = userName; for (NSXMLElement *groupElement in itemElement.children) {
//组名 12
NSString *groupString = groupElement.stringValue; NSMutableArray *users = results[groupString]; if (users == nil) {
users = [NSMutableArray array];
results[groupString] = users;
}
[users addObject:user];
}
} if (self.fentchFriendList) {
self.fentchFriendList(results);
} NSLog(@"获取好友列表 %@",results);
return YES;
}
//监听外部传过来的数据,就是如果有好友发来信息,则,回调此方法.
/**
//传过来很多message标签,我们只需要带body的message标签
<message xmlns="jabber:client" type="chat" id="purplea775e98a" to="1234@zy" from="gxb1@zy/MacdeMacBook-Pro-3"><composing xmlns="http://jabber.org/protocol/chatstates"/></message>
2015-11-18 09:35:20.264 WeChat[3162:161262] message is <message xmlns="jabber:client" type="chat" id="purplea775e98b" to="1234@zy" from="gxb1@zy/MacdeMacBook-Pro-3"><active xmlns="http://jabber.org/protocol/chatstates"/><body>sad</body></message>
2015-11-18 09:35:30.491 WeChat[3162:161262] message is <message xmlns="jabber:client" type="chat" id="purplea775e98c" to="1234@zy" from="gxb1@zy/MacdeMacBook-Pro-3"><active xmlns="http://jabber.org/protocol/chatstates"/></message>
*/
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
//判断当前的message标签是否带body子标签
if ([message isChatMessageWithBody]) {
NSString *body = message.body;
//获取发信息的人的jid
XMPPJID *jidFrom = message.from; NSDictionary *messageDic = @{
@"fromUser":jidFrom.description,
@"text":body
}; //通知,发出接收消息的通知
[[NSNotificationCenter defaultCenter] postNotificationName:kRecevieMessageNotification object:nil userInfo:messageDic]; } }
//注册成功调用的方法
- (void)xmppStreamDidRegister:(XMPPStream *)sender {
_connectType = kTypeLogin;
self.loginSuccessBlock = self.registerBlock;
self.registerBlock = nil;
[self xmppStreamDidConnect:sender];
} @end

把宏定义改成自己的参数,在相关的UI操作代码中创建  单例XMPPHelper  然后调用相关方法,并在相关的代理方法中实现必要的操作就可以了。

iOS中 XMPP即时通讯实现的主要步骤的更多相关文章

  1. iOS中XMPP简单聊天实现 好友和聊天

    版权声明本文由陈怀哲首发自简书:http://www.jianshu.com/users/9f2e536b78fd/latest_articles;微信公众号:陈怀哲(chenhuaizhe2016) ...

  2. XMPP即时通讯资料记录

    几天开始研究XMPP即时通讯的技术,来实现移动应用的计时聊天功能.记录下参考的博客地址,还挺详细的. http://blog.csdn.net/fhbystudy/article/details/16 ...

  3. iOS开发之XMPP即时通讯简单实现

    首先搭载服务器和数据库 搭载服务器我用的是openfire,数据库用的是mysql 这里推荐两个链接 配置mysql,用的是mysql workbench http://justsee.iteye.c ...

  4. xmpp即时通讯的笔记(摘抄)

    xmpp的使用: 即时通讯 instant messaging(IM) :  -->实时收发信息! 即时通讯相关软件: **QQ,MSN,GoogleTalk,AIM,Jabber(XMPP别名 ...

  5. XMPP即时通讯

    XMPP:XMPP是基于XML的点对点通讯协议,The Extensible Messaging and Presence Protocol(可扩展通讯和表示协议). XMPP可用于服务类实时通讯,表 ...

  6. iOS开发之即时通讯之Socket(AsyncSocket)

    1.AsyncSocket介绍 如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯. iOS中Socket编程的方式: BSD Socket: BSD Socket 是UNIX系统中 ...

  7. XMPP即时通讯基础知识

    XMPP参考 一.定义 XMPP 是一种很类似于http协议的一种数据传输协议,它的过程就如同“解包装--〉包装”的过程,用户只需要明白它接受的类型,并理解它返回的类型,就可以很好的利用xmpp来进行 ...

  8. XMPP即时通讯协议使用(前传)——协议详解

    XMPP详解 XMPP(eXtensible Messaging and Presence Protocol,可扩展消息处理和现场协议)是一种在两个地点间传递小型结构化数据的协议.在此基础上,XMPP ...

  9. XMPP即时通讯协议使用(七)——利用Strophe实现WebIM及strophe.plugins插件使用

    Strophe简介与Openfire配置 Strophe.js是为XMPP写的一个js类库.因为http协议本身不能实现持久连接,所以strophe利用BOSH模拟实现持久连接. 官方文档: http ...

随机推荐

  1. UGUI Image血条或者进度条效果

    把图片的Image组件中的image type选成Filled,下面就出现了Fill Amount,通过GetComponent<Image>().fillAmount; 就可以拿到flo ...

  2. sysbench测试MySQL筛选tps

    log=$1tps_array=`awk -F '[,:]' '{print $4}' ${log}`zero=0 for tps in ${tps_array}do tps=`echo ${tps} ...

  3. Murano Deployment

    2015-09-14 05:53:02 — Action deploy is scheduled 2015-09-14 05:53:03 — Unable to load due to 'could ...

  4. BoostrapTable-本地模式(一次性加在所有数据)

    直接上代码 数据: [ { "id": "1001", "name": "yyq", "isAdmin&quo ...

  5. spring的IOC和AOP详细讲解

    1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...

  6. Python 科学工具使用

    Python 科学工具笔记 numpy a = numpy.array([1,2,3,4]);// 创建一个numpy的数组对象 此时a.shape显示的值为(4,); 由此得出结论在一维的数组中, ...

  7. golang中并发的相关知识

    golang中done channel理解:https://segmentfault.com/a/1190000006261218 golang并发模型之使用Context:https://segme ...

  8. Intellij IDEA 配置jrebel热部署

    Jrebel 先介绍一下jrebel,jrebel是可以热部署项目的一个工具,更改代码自动部署并不需要重启项目(在spring中的controller中,增加.修改方法都是可以进行热部署而不需要重启的 ...

  9. Java基础入门 - 标识符及其命名规范

    类名.变量名.方法名都称为标识符 标识符命名规范: 由字母(A-Z或a-z).数字.下划线(_)和美元符($)中的一种或多种组合而成 不可以数字开头 大小写敏感 关键字不能用作标识符 合法标识符如:D ...

  10. poj 2378 删点最大分支不超过一半

    http://poj.org/problem?id=2378 这题和找重心基本一样,判断条件换一下就行 #include <iostream> #include <string> ...