XMPP用户登录

XMPP用户登录


技术博客http://www.cnblogs.com/ChenYilong/ 
新浪微博http://weibo.com/luohanchenyilong 



 















XMPP用户登录
 

技术博客http://www.cnblogs.com/ChenYilong/新浪微博http://weibo.com/luohanchenyilong
 


XMPP核心文件,基于TCPXML流的传输 

 XMPPStream:是开发过程中最主要交互的类,所有扩展和自定义代 
码均要基于此类进行 
 XMPPParser:供XMPPStream解析使用 
 XMPPJID:提供了一个不可变JID的实现,遵守NSCopying协议和 NSCoding协议 
 XMPPElement:以下三个XMPP元素的基类 
 XMPPIQ :请求(加好友) 
 XMPPMessage :消息 
 XMPPPresence :出席(标示用户的在线状态) 
 XMPPModule:开发XMPP扩展时使用 
 XMPPLogging:XMPP的日志框架 
 XMPPInternal:整个XMPP框架内部使用的核心和高级底层内容 



XMPP用户登录的实现步骤

 XMPPFrame框架是通过代理的方式实现消息传递的 
 实现用户登录的步骤如下: 
 1. 实例化XMPPStream并设置代理,同时添加代理到工作队列 
 2. 使用JID连接至服务器,默认端口为5222,JID字符串中需要包含服 务器的域名 
 3. 完成连接的代理方法中验证用户密码,连接完成后XMPPStream isConnect属性为YES 
 4. 验证代理方法中判断用户是否登录成功 
 5. 上线或者下线成功后,向服务器发送Presence数据,以更新用户在 服务器的状态 


AppDelegate.h

 为了简化开发,XMPP的引用程序通常会将XMPPStream放置在
AppDelegate中,以便于全局访问 AppDelegate中添加以下属性和方法定义
@property (strong, nonatomic, readonly)XMPPStream *xmppStream; - (void)connect;
- (
void)disconnect; 


XMPPStream私有方法--设置代理及通知状态

//设置XMPPStream
- (void)setupStream { 
_xmppStream = [[XMPPStream alloc] init]; 
[_xmppStream addDelegate:selfdelegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)]; 

} 
// XMPPStream上线 - (void)goOnline XMPPPresence[_xmppStream
} 
{
*presence = [
XMPPPresence presence];sendElement:presence]; 
// XMPPStream离线 
- (void)goOffline { 
XMPPPresence *presence = [XMPPPresencepresenceWithType:@"unavailable"]; 
[_xmppStream sendElement:presence]; } 

connect方法

- (void)connect {
// 1. 设置XMPPStream [self setupStream];
// 2. 设置用户名、密码及服务器 // 3. 设置XMPPStream信息 
[_xmppStream setMyJID:[XMPPJIDjidWithString:userName]]; [_xmppStreamsetHostName:server]; 
_myPassword = password;
// 4. 连接至服务器,如果没有指定jidhostName,连接才会报错 
NSError *error = nil;
[
_xmppStreamconnectWithTimeout:XMPPStreamTimeoutNoneerror:&error]; if (error) { 
NSLog(@"连接错误:%@", error.localizedDescription); } 
} 


disconnect方法

- (void)disconnect { 
// 1. 发送离线状态 [self goOffline];
// 2. XMPPStream断开连接 [_xmppStreamdisconnect]; 
} 


XMPP代理方法

#pragma mark - XMPPStream代理方法 
#pragma mark
 连接到服务器 
- (void)xmppStreamDidConnect:(XMPPStream *)sender { 
// 1. 验证密码 NSError *error = nil; 
[_xmppStream authenticateWithPassword:_myPassworderror:&error]; if (error) { 
NSLog(@"身份验证错误:%@", error.localizedDescription); } 
} 
#pragma mark 通过验证 
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { 
[self goOnline]; } 
#pragma mark 验证失败 
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error { 
NSLog(@"验证失败:%@", error); } 


在应用程序状态切换时连接或断开连接

#pragma mark 应用程序注销激活状态 - (void)applicationWillResignActive:(UIApplication *)application { 
// 断开连接 
[self disconnect]; } 

#pragma mark 应用程序重新被激活 
- (void)applicationDidBecomeActive:(UIApplication *)application { 
// 连接 
[self connect]; } 


AppDelegate中添加访问助手方法

#pragma mark - AppDelegate访问助手方法 /** 
AppDelegate的访问助手 
*/ 
- (
AppDelegate *)appDelegte { 
return (AppDelegate *)[[UIApplicationsharedApplication] delegate]; 
} 
/** AppDelegateXMPPStream属性访问助手 */ 
- (XMPPStream *)xmppStream { 
return [[self appDelegte] xmppStream]; } 


© chenyilong. Powered by Postach.io


作者:
出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
本文版权归作者和博客园共有,欢迎转载,
但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/ChenYilong/p/3587335.html