一.目的以及效果:
用Xcode利用xmpp框架建立客户端,实现向服务器注册添加用户 密码,以及登陆,离线状态
工程的主要结构:新建singleview工程,用xib拖放两个输入框和两个按钮,
并在viewcontroller中进行关联,建立regisgtviewcontroller同时选中xib,利用xib关联
最终效果在网页Openfire管理控制台中可以查看
二.具体过程:
关键框架:#import "XMPPFramework.h"
建立一个单例viewcontroller
单例部分:
.h文件中
//单例的初始化方法
+(instancetype)shareManager;
//登陆的方法
-(void)loginWithName:(NSString *)name andPassword:(NSString *)password block:(void(^)(NSString *name,NSString *password,int resultType))block;
//注册方法
-(void)registerWithName:(NSString *)name andPassword:(NSString *)password block:(void(^)(NSString *name,NSString *password,int resultType))block;
//注销
-(void)logOut;
.m文件中
//(自己写的)类的拓展 私有方法
@interface XMPPManager1()<XMPPStreamDelegate>
{
//用block用来回调处理
__block void(^_block)(NSString *name,NSString *password,int resultType);
}
@property(nonatomic,strong)XMPPStream *stream;
//记录用户名 密码
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *password;
//设置bool值 登陆和注册的方法不同 需要判断
@property(nonatomic,assign)int type;
@end
@implementation XMPPManager1
//单例的初始化方法
+(instancetype)shareManager
{
static XMPPManager1 *share = nil;
static dispatch_once_t onecToken;
dispatch_once(&onecToken, ^{
share = [[XMPPManager1 alloc]init];
});
return share;
}
//只初始化一次
-(id)init
{
if ([super init]) {
//初始化xmppstream
self.stream = [[XMPPStream alloc]init];
//设置服务器的地址
self.stream.hostName = @"127.0.0.1";
//设置端口
self.stream.hostPort = 5222;
//设置代理
[self.stream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
return self;
}
//登陆的方法
-(void)loginWithName:(NSString *)name andPassword:(NSString *)password block:(void(^)(NSString *name,NSString *password,int resultType))block
{
self.type = 1;//登陆
self.name = name;//记录用户
self.password = password;//记录密码
_block = block;
//设置用户
XMPPJID *jid = [XMPPJID jidWithUser:name domain:@"wuxiaoyuan.local" resource:@"iphone"];
self.stream.myJID = jid;
//调用连接方法
[self connectToServer];
}
//提取出连接服务器的方法
-(void)connectToServer
{
//再次点击 处于连接状态会断开连接
if ([self.stream isConnected]) {
[self logOut];
}
NSError *error = nil;
[self.stream connectWithTimeout:XMPPStreamTimeoutNone error:&error];
if (error) {
NSLog(@"%@",error);
}
}
//注册方法
-(void)registerWithName:(NSString *)name andPassword:(NSString *)password block:(void(^)(NSString *name,NSString *password,int resultType))block
{
self.type = 2;//注册
self.name = name;//记录用户
self.password = password;//记录密码
_block = block;
//设置用户
XMPPJID *jid = [XMPPJID jidWithUser:name domain:@"wuxiaoyuan.local" resource:@"iphone"];
self.stream.myJID = jid;
//调用连接方法
[self connectToServer];
}
//注销
-(void)logOut
{
//改变用户状态 为离线
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[self.stream sendElement:presence];
//断开连接
[self.stream disconnect];
}
#pragma mark --代理方法
//连接失败
-(void)xmppStreamConnectDidTimeout:(XMPPStream *)sender
{
NSLog(@"连接失败");
if (_block) {
_block(self.name,self.password,-1);
}
//需要断开连接
[self.stream disconnect];
}
//连接成功
-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
if (self.type == 1) {
//向服务器发送密码验证 (验证成功和验证失败)
[self.stream authenticateWithPassword:self.password error:nil];
}else if(self.type == 2)
{
//向服务器发送密码进行注册 (验证成功和验证失败)
[self.stream registerWithPassword:self.password error:nil];
}
}
//----------------------
//验证失败
-(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
NSLog(@"验证失败哦");
if (_block) {
_block(self.name,self.password,0);
}
//需要断开连接
[self.stream disconnect];
}
//验证成功
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
//改变用户状态 为在线
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[self.stream sendElement:presence];
}
//----------------------
//----------------------
//注册失败
-(void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error
{
NSLog(@"注册失败");
if (_block) {
_block(self.name,self.password,0);
}
//需要断开连接
[self.stream disconnect];
}
//注册成功
-(void)xmppStreamDidRegister:(XMPPStream *)sender
{
NSLog(@"注册成功");
if (_block) {
_block(self.name,self.password,1);
}
//需要断开连接
[self.stream disconnect];
}
//----------------------
在其他的controller中调用单例
关键语句:
[[XMPPManager1 shareManager]registerWithName:self.userField.text andPassword:self.passwordField.text block:^(NSString *name, NSString *password, int resultType) {
if (resultType == 1) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}];