XMPP iOS客户端实现三:登录、注册
1.创建一个单例模式来管理xmpp的连接和操作
+(XMPPManager *)share
{
static XMPPManager *_share=nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_share=[[XMPPManager alloc] init];
});
return _share;
}
-(id)init
{
if (self=[super init]) {
[self setupStream];
}
return self;
}
- (void)setupStream
{
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppStream setHostName:@"127.0.0.1"];
[xmppStream setHostPort:];
}
2.登录和注册都需要使用用户名进行连接服务器,
-(void)login
{
[self connect];
}
-(void)reg
{
isReg=YES;
[self connect];
}
- (BOOL)connect
{
if (xmppStream.isConnected) {
[self disconnect];
}
if (![xmppStream isDisconnected]) {
return YES;
} if (self.userName == nil) {
return NO;
} [xmppStream setMyJID:[XMPPJID jidWithString:self.userName]]; NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"
message:@"See console for error details."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show]; return NO;
} return YES;
}
3.在连接代理中判断是登录还是注册,发送不同命令
//成功连接服务器
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
//连接服务器失败
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); }
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); NSError *error = nil;
if (isReg)
{
if (![xmppStream registerWithPassword:self.pwd error:&error])
{ }
}
else
{
if (![xmppStream authenticateWithPassword:self.pwd error:&error])
{ }
} }
4.处理登录或注册代理函数
//登录成功
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); [self goOnline];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"登录成功"
message:@"登录成功"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}
//登录失败
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"密码错误"
message:@"密码错误"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show]; }
//注册成功
- (void)xmppStreamDidRegister:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册成功"
message:@"注册成功"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
isReg=NO;
}
//注册失败
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
NSLog(@"%@",[[error elementForName:@"error"] stringValue]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"注册失败"
message:@"注册失败"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
isReg=NO;
}


5.登录或注册的用户名必须“username@主机名”这种格式

转载请注明:http://www.cnblogs.com/bandy/p/3756492.html
XMPP iOS客户端实现三:登录、注册的更多相关文章
- iOS开发一个用户登录注册模块需要解决的坑
最近和另外一位同事负责公司登录和用户中心模块的开发工作,开发周期计划两周,减去和产品和接口的协调时间,再减去由于原型图和接口的问题,导致强迫症纠结症状高发,情绪不稳定耗费的时间,能在两周基本完成也算是 ...
- 环信 之 iOS 客户端集成三:基础功能
SDK中,大部分与网络有关的操作,都有三种方法: 同步方法 通过delegate回调的异步方法.要想能收到回调,必须要注册为:[[EaseMob sharedInstance].chatManager ...
- XMPP iOS客户端实现二:xcode项目配置
1.下载XMPPFramework,下载地址:https://github.com/robbiehanson/XMPPFramework 2.创建项目并将XMPP库引入: 3.添加需要的库文件: 4. ...
- XMPP iOS客户端实现一:服务器
1.下载ejabberd,下载链接http://www.process-one.net/en/ejabberd/downloads/ 2.安装,使用默认配置即可,next.. 3.启动ejabberd ...
- 开源中国iOS客户端学习
开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客 http://blog.csdn.net/column/details/xfzl-kykhd.html ...
- 7. Swift 基于Xmpp和openfire实现一个简单的登录注册
1. 基本步骤:首先导入Xmpp框架,配置环境 ->由于我们使用的是OC的Xmpp框架,再进行Swift开发时需要进行桥接. 具体方法就是创建一个基于c的.h的头文件,然后将我们需要编译OC的语 ...
- SpringBoot注册登录(三):注册--验证账号密码是否符合格式及后台完成注册功能
SpringBoot注册登录(一):User表的设计点击打开链接SpringBoot注册登录(二):注册---验证码kaptcha的实现点击打开链接 SpringBoot注册登录(三):注册 ...
- XMPPFrameWork IOS 开发(三)登录
原始地址:XMPPFrameWork IOS 开发(三) XMPP中常用对象们: XMPPStream:xmpp基础服务类 XMPPRoster:好友列表类 XMPPRosterCoreDataSto ...
- 一个关于vue+mysql+express的全栈项目(三)------ 登录注册功能的实现(已经密码安全的设计)
本系列文章,主要是一个前端的视角来实现一些后端的功能,所以不会讲太多的前端东西,主要是分享做这个项目学到的一些东西,,,,, 好了闲话不多说,我们开始搭建后端服务,这里我们采用node的express ...
随机推荐
- CPU状态信息us,sy,ni,id,wa,hi,si,st含义
转自:http://blog.csdn.net/sasoritattoo/article/details/9318893 转自:http://fishermen.iteye.com/blog/1995 ...
- openmp 的使用
http://blog.csdn.net/gengshenghong/article/details/7003110 说明:这部分内容比较基础,主要是分析几个容易混淆的OpenMP函数,加以理解. ( ...
- CURL 模拟http提交
1:CURL模拟get提交 private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETUR ...
- D3中数据与DOM element绑定之data() enter() exit()浅析
几个非常有用的links: [1] three little circles. http://bost.ocks.org/mike/circles/ [2] How selection works. ...
- BZOJ 4066 简单题 ——KD-Tree套替罪羊树
[题目分析] 直接x,y二维轮番划分,暴力即可. 套上替罪羊,打碎重构,对于时间复杂度有了保证. 写起来好麻烦,重构的技巧很棒! [代码] #include <cstdio> #inclu ...
- log4j 配置INFO 和DEBUG 分布输出至两个文件
博客地址: http://blog.csdn.net/wangchsh2008/article/details/8812857
- js开发笔记
jQuery jQuery判断页面元素是否存在:$("#someID").length > 0 AJAX 通过设置window.location.hash值和响应window ...
- ACM ICPC Vietnam National Second Round
A. Stock Market 枚举哪一天买入,哪一天卖出即可. #include<cstdio> #include<algorithm> using namespace st ...
- [Leetcode] Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [BZOJ1127][POI2008] KUP子矩阵
Description 给一个n*n的地图,每个格子有一个价格,找一个矩形区域,使其价格总和位于[k,2k] Input 输入k n(n<2000)和一个n*n的地图 Output 输出矩形的左 ...