即时通讯2
2016-05-18 本文已影响19人
YuWenHaiBo
接着上文,现在我们环境都有了,可以写代码了
先导入并配置好xmpp的环境,我们可以用cocoapod配置好当然我们也可以手动配置具体配置,这里不说了
在storyBoard里面建立如下界面,分别是登录控制器和注册控制器
下面我们先实现登录功能
先创建个xmppmanager单例
.m 文件有这些
#import "XMPPManager.h"
typedef NS_ENUM(NSInteger ,connectToServerPurpose) {
ConnectToServerPurposeOfLogin ,
ConnectToServerPurposeOfRegist,
};
@interface XMPPManager ()<XMPPStreamDelegate>
/// 用户名
@property(nonatomic, strong) NSString *userName;
/// 密码
@property(nonatomic, strong) NSString *password;
/// 记录所处的状态
@property(nonatomic) connectToServerPurpose connectToServer;
/// 用户名
@property(nonatomic, strong) NSString *registerUserName;
/// 密码
@property(nonatomic, strong) NSString *registerPassword;
@end
static XMPPManager *xmpp = nil ;
@implementation XMPPManager
// 实现登录的方法
- (void)loginWithUserName:(NSString *)name password:(NSString *)password
{
self.userName = name;
self.password = password;
// 记录登录状态
self.connectToServer = ConnectToServerPurposeOfLogin;
// 连接服务器
[self linkServer];
}
// 实现注册的方法
- (void)registWithUserName:(NSString *)name password:(NSString *)password
{
self.registerUserName = name;
self.registerPassword = password;
// 记住注册状态
self.connectToServer = ConnectToServerPurposeOfRegist;
//连接服务器
[self linkServer];
}
// 连接服务器
- (void)linkServer
{
// 具体区分是登录还是注册
NSString *string = [NSString string];
switch (self.connectToServer) {
case ConnectToServerPurposeOfLogin:
string = [NSString stringWithFormat:@"%@",self.userName];
break;
case ConnectToServerPurposeOfRegist:
string = [NSString stringWithFormat:@"%@",self.registerUserName];
break;
default:
break;
}
// 要连接服务器,要有用户的身份认证
// 身份证
// 参数一: 用户名
// 参数二: 域名
// 参数三: 资源
XMPPJID *jid = [XMPPJID jidWithUser:string domain: kDomin resource:kResource];
// 在通道上进行身份验证
self.xmppStream.myJID = jid;
// 如果当前聊天工具处于连接状态或者已经连接状态,此时,你需要切断连接(此逻辑不唯一)
if ([self.xmppStream isConnected]||[self.xmppStream isConnecting]) {
[self disConnectToserver];
}
NSError *error = nil;
// 连接超时
[self.xmppStream connectWithTimeout:30 error:&error];
if (error) {
NSLog(@"服务器连接超时");
}
}
// 断开连接
- (void)disConnectToserver
{
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
// 告诉通道下线了
[self.xmppStream sendElement:presence];
// 使通道断开连接
[self.xmppStream disconnect];
}
+ (XMPPManager *)shareManaged
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
xmpp = [[XMPPManager alloc] init];
});
return xmpp;
}
#pragma mark - xmppdelegate
// 连接超时
- (void)xmppStreamConnectDidTimeout:(XMPPStream *)sender
{
NSLog(@"连接超时");
}
// 连接成功
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
// 具体区分是登录还是注册
NSString *string = [NSString string];
switch (self.connectToServer) {
case ConnectToServerPurposeOfLogin:
{ string = [NSString stringWithFormat:@"%@",self.password];
[self.xmppStream authenticateWithPassword:string error:nil];}
break;
case ConnectToServerPurposeOfRegist:
{
string = [NSString stringWithFormat:@"%@",self.registerPassword];
[self.xmppStream registerWithPassword:string error:nil];
}
break;
default:
break;
}
// 密码的验证
NSLog(@"---连接成功");
}
// 当通道断开连接
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
// 1.手动断开
// 2. 被动断开
if(error)
{
NSLog(@"断开连接,error = %@",error);
}
}
//认证失败
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
NSLog(@"认证失败,error = %@",error);
}
// 认证成功
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
// 上线
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[self.xmppStream sendElement:presence];
}
#pragma mark -1 创建通道
- (instancetype)init
{
self = [super init];
if (self) {
// 初始化通道
self.xmppStream = [[XMPPStream alloc] init];
// 设置openfire服务器的地址
self.xmppStream.hostName = kHostName;
// 设置端口号
self.xmppStream.hostPort = kHostPort;
// 添加代理
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
return self;
}
@end
登录的.m
#import "LoginViewController.h"
#import "XMPPManager.h"
#import "MainTableViewController.h"
@interface LoginViewController ()<XMPPStreamDelegate>
@property (weak, nonatomic) IBOutlet UITextField *userTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@end
@implementation LoginViewController
#pragma mark - 登录按钮的响应方法
// 登录点击事件
- (IBAction)buttonClick:(id)sender {
XMPPManager *xmppManager = [XMPPManager shareManaged];
[xmppManager loginWithUserName:self.userTextField.text password:self.passwordTextField.text];
[[XMPPManager shareManaged].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//已经连接上了
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
MainTableViewController *mainVC = [[MainTableViewController alloc] init];
[self.navigationController pushViewController:mainVC animated:YES];
}
@end
注册的.m文件
#import "RegistViewController.h"
#import "XMPPManager.h"
@interface RegistViewController () <XMPPStreamDelegate>
@property (weak, nonatomic) IBOutlet UITextField *registUserName;
@property (weak, nonatomic) IBOutlet UITextField *registPassword;
@end
@implementation RegistViewController
#pragma mark - 注册按钮的实现方法
- (IBAction)registAction:(id)sender {
XMPPManager *xmppmanager = [XMPPManager shareManaged];
[xmppmanager registWithUserName:self.registUserName.text password:self.registPassword.text];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置代理
[[XMPPManager shareManaged].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
#pragma mark - XMPPStreamDelegate
// 注册成功会调用
- (void)xmppStreamDidRegister:(XMPPStream *)sender
{
NSLog(@"注册成功");
[self dismissViewControllerAnimated:YES completion:nil];
}
// 注册失败会调用
- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error{
NSLog(@"注册失败");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end