简洁(chou)的登录页面
登录页面.jpeg
有账号直接登录,没有账号注册并登录。接下来直接上相关代码和简单注解!
1、设置聊天服务相关参数
#pragma mark ==========XMPP相关值设置==========
#define XMPP_HOST @"xxx.xx.x.xx"//服务器IP地址
#define XMPP_PORT 5222 //服务器端口号,默认就是5222
#define XMPP_DOMAIN @"127.0.0.1"//我自己电脑的域名
#define XMPP_SUBDOMAIN @"conference"//muc房间相关
#define XMPP_RESOURCE @"iOS" //这个我创建xmppjid的时候没用到
2、创建XMPPManager类
XMPPManager
类主要是用于管理xmpp相关的一些对象包括XMPPStream、XMPPRoster、XMPPJID等(以后章节会逐一加进去)以及一些代理回调等等。
static XMPPManager *_instance;
//单例创建XMPPManager对象,用于管理xmpp一些初始化啊,各种回调啊的一些处理
+ (instancetype)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [XMPPManager new];
});
return _instance;
}
3、设置相关代理属性及回调(仅显示登录注册相关的代理和属性)
//代理
XMPPStreamDelegate
#pragma mark ==========XMPPStream部分实现代理回调==========
//socket 连接建立成功
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket;
//数据流初始化成功,建立连接,连接之后可以做注册和登录操作
- (void)xmppStreamDidConnect:(XMPPStream *)sender;
//数据流连接失败,断开连接
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error;
//登录失败
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error;
//登录成功
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender;
//注册成功
-(void)xmppStreamDidRegister:(XMPPStream *)sender;
//注册失败
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error;
//接收到XMPPPresence回调
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence;
//接收到消息回调
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message;
//接收到IQ回调
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq;
//接收到error信息回调
- (void)xmppStream:(XMPPStream *)sender didReceiveError:(NSXMLElement *)error;
//属性
@property (nonatomic, strong) XMPPStream * __nullable xmppStream;
@property (nonatomic, strong) XMPPJID *myXmppJID;
//变量
@implementation XMPPManager{
NSString *passWord;
BOOL xmppNeedRegister;//用于判断用户是否需要注册
}
4、初始化xmppStream对象以及和服务器连接
注册、登录、发消息、加好友等等所有相关操作都是基于xmppStream连接之后才能做得操作,所以我们先要将xmppStream连接成功。
//懒加载xmppStream
- (XMPPStream *)xmppStream{
if (!_xmppStream) {
_xmppStream = [[XMPPStream alloc] init];
//socket 连接的时候 要知道host port 然后connect
[self.xmppStream setHostName:XMPP_HOST];
[self.xmppStream setHostPort:XMPP_PORT];
//添加代理
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
return _xmppStream;
}
每一个用户都需要有一个唯一的XMPPJID
[XMPPJID jidWithUser:self.username.text domain:XMPP_DOMAIN resource:nil]
/**
用户注册登录
@param JID 用户的XMPPJID
@param password 登录密码
@param needRegister 是否需要注册
*/
- (void)loginWithJID:(XMPPJID *)JID withPassWord:(NSString *)password needRegister:(BOOL)needRegister{
self.myXmppJID = JID;
passWord = password;
xmppNeedRegister = needRegister;
//如果已经存在一个连接,需要将当前的连接断开,然后再开始新的连接
if ([self.xmppStream isConnected]) {
[self logout];
}
[self.xmppStream setMyJID:JID];
NSError *error;
//xmppStream连接服务器
[self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error];
NSLog(@"%@",error);
}
5、在连接成功回调中进行用户注册/登录
//数据流初始化成功,建立连接
- (void)xmppStreamDidConnect:(XMPPStream *)sender{
if (xmppNeedRegister) {//这个项目中不需要注册,直接登录就行
//注册服务,再注册成功的回调中调用authenticateWithPassword完成验证登录
[self.xmppStream registerWithPassword:passWord error:nil];
} else {//登录,认证
[self.xmppStream authenticateWithPassword:passWord error:nil];
}
}
登录成功之后会回调
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
;注册称重之后会回调-(void)xmppStreamDidRegister:(XMPPStream *)sender
,如果注册成功之后需要直接登录的话,可以在此回调直接[self.xmppStream authenticateWithPassword:passWord error:nil];
进行用户密码验证,代码如下:
//登录成功
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
[self goOnline];
}
//登录成功之后就上线
- (void)goOnline{
// 发送一个<presence/> 默认值avaliable 在线 是指服务器收到空的presence 会认为是这个
// status ---自定义的内容,可以是任何的。
// show 是固定的,有几种类型 dnd、xa、away、chat,在方法XMPPPresence 的intShow中可以看到
XMPPPresence *presence = [XMPPPresence presence];
[presence addChild:[DDXMLNode elementWithName:@"status" stringValue:@"我现在很忙"]];
[presence addChild:[DDXMLNode elementWithName:@"show" stringValue:@"xa"]];
[self.xmppStream sendElement:presence];
//发送上线通知
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_LoginSuccess object:nil];
}
//注册成功
-(void)xmppStreamDidRegister:(XMPPStream *)sender{
//注册成功之后直接登录
[self.xmppStream authenticateWithPassword:passWord error:nil];
}
如果注册失败或者登录失败的话也有相关回调,自行打印查看错误原因。
//登录失败
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error{
[Utils showMessageAlertWithTitle:@"登录失败" message:error.description];
NSLog(@"%@",error);
}
//注册失败
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error{
[Utils showMessageAlertWithTitle:@"注册失败" message:error.description];
NSLog(@"%@",error);
}