ios开发-XMPP功能的实现
来源:互联网 发布:淘宝客优惠券怎么推广 编辑:程序博客网 时间:2024/06/08 23:43
在代理中实现
#import <UIKit/UIKit.h>
#import "XMPPManager.h"
//自定义一个用来接受xmppStreamDelegate的协议
@interface MYYAppDelegate : UIResponder <UIApplicationDelegate,XMPPStreamDelegate]] >
@property (strong, nonatomic) UIWindow *window;
@end
#import "XMPPManager.h"
//自定义一个用来接受xmppStreamDelegate的协议
@interface MYYAppDelegate : UIResponder <UIApplicationDelegate,XMPPStreamDelegate]] >
@property (strong, nonatomic) UIWindow *window;
@end
#import "MYYAppDelegate.h"
@implementation MYYAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//如果是第一次登录,也就是说输入框的内容都是空的,就进行写入操作
if ([[NSUserDefaults standardUserDefaults]objectForKey:@"userName"] == nil) {
//初始化一个storyboard 第一个参数是storyboard的名字 第二个参数是文件路径,如果是nil 默认是在main bundle中(bundle 可以先为nil)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LoginAndRegister" bundle:nil];
//通过storyboard 实例化一个对象
UINavigationController *naVC = [storyboard instantiateInitialViewController];
//让窗口可见进行
[self.window makeKeyAndVisible];
//模态出登录页面
[self.window.rootViewController presentViewController:naVC animated:NO completion:nil];
}else{
//如果不是第一次登录
//程序自己给它完成一次登录
//添加代理,
[[XMPPManager defaultXMPPManager].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//取出用户名和密码
NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
//系统帮助在后台登录
[[XMPPManager defaultXMPPManager]loginWithUser:userName andPassWord:password];
}
return YES;
}
//验证登录密码成功
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{ NSLog(@"欢迎,验证成功!!!");
//验证成功后让客户端上线
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[[XMPPManager defaultXMPPManager].xmppStream sendElement:presence];
}
//验证登录密码失败的方法
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(@"对不起,验证失败 = %@!!!",error);
}
@implementation MYYAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//如果是第一次登录,也就是说输入框的内容都是空的,就进行写入操作
if ([[NSUserDefaults standardUserDefaults]objectForKey:@"userName"] == nil) {
//初始化一个storyboard 第一个参数是storyboard的名字 第二个参数是文件路径,如果是nil 默认是在main bundle中(bundle 可以先为nil)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LoginAndRegister" bundle:nil];
//通过storyboard 实例化一个对象
UINavigationController *naVC = [storyboard instantiateInitialViewController];
//让窗口可见进行
[self.window makeKeyAndVisible];
//模态出登录页面
[self.window.rootViewController presentViewController:naVC animated:NO completion:nil];
}else{
//如果不是第一次登录
//程序自己给它完成一次登录
//添加代理,
[[XMPPManager defaultXMPPManager].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//取出用户名和密码
NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
NSString *password = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
//系统帮助在后台登录
[[XMPPManager defaultXMPPManager]loginWithUser:userName andPassWord:password];
}
return YES;
}
//验证登录密码成功
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{ NSLog(@"欢迎,验证成功!!!");
//验证成功后让客户端上线
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[[XMPPManager defaultXMPPManager].xmppStream sendElement:presence];
}
//验证登录密码失败的方法
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(@"对不起,验证失败 = %@!!!",error);
}
#import <UIKit/UIKit.h>
#import "XMPPManager.h"
@interface LoginViewController : UIViewController<XMPPStreamDelegate]] >
//用户输入框
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
//密码输入框
@property (weak, nonatomic) IBOutlet UITextField *passField;
#import "XMPPManager.h"
@interface LoginViewController : UIViewController<XMPPStreamDelegate]] >
//用户输入框
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
//密码输入框
@property (weak, nonatomic) IBOutlet UITextField *passField;
#import "LoginViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//在登录页面给通信管道类添加代理
[[XMPPManager defaultXMPPManager].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)LoginButton:(id)sender {
NSString *userName = self.userNameField.text;
NSString *password = self.passField.text;
[[XMPPManager defaultXMPPManager]loginWithUser:userName andPassWord:password];
}
#pragma mark -xmppStreamDelegate
//实现xmppstream 代理协议
//验证登录密码成功
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
NSLog(@"欢迎,验证成功!!!");
//验证成功后让客户端上线
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[[XMPPManager defaultXMPPManager].xmppStream sendElement:presence];
//如果第一次登录成功就保存密码
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.userNameField.text forKey:@"userName"];
[userDefaults setObject:self.passField.text forKey:@"password"];
//写入本地 获取到用户名和密码要立刻存入(必须要存入,不然容易出现问题,输入几次都不成功)
[userDefaults synchronize];
//登录成功的时候就推出下一页面
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
//验证登录密码失败的方法
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
NSLog(@"对不起,验证失败 = %@!!!",error);
}
@end
#import <UIKit/UIKit.h>
@interface RegisterViewController : UIViewController
@end
@interface RegisterViewController : UIViewController
@end
#import "RegisterViewController.h"
#import "XMPPManager.h"
@interface RegisterViewController ()<XMPPStreamDelegate]] >
//用户名输入框
@property (weak, nonatomic) IBOutlet UITextField *userName;
//密码输入框
@property (weak, nonatomic) IBOutlet UITextField *password;
@end
@implementation RegisterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//添加代理对象
[[XMPPManager defaultXMPPManager].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)RegisterButton:(id)sender {
NSString *userName = self.userName.text;
NSString *password = self.password.text;
[[XMPPManager defaultXMPPManager]registerWithUser:userName andPassWord:password];
}
#pragma mark -XMPPStreamDelegate
//验证注册成功
-(void)xmppStreamDidRegister:(XMPPStream *)sender{
NSLog(@"验证注册成功");
[self.navigationController popToRootViewControllerAnimated:YES];
}
//验证注册失败
-(void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error{
NSLog(@"验证注册失败= %@",error);
}
@end
#import "XMPPManager.h"
@interface RegisterViewController ()<XMPPStreamDelegate]] >
//用户名输入框
@property (weak, nonatomic) IBOutlet UITextField *userName;
//密码输入框
@property (weak, nonatomic) IBOutlet UITextField *password;
@end
@implementation RegisterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//添加代理对象
[[XMPPManager defaultXMPPManager].xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)RegisterButton:(id)sender {
NSString *userName = self.userName.text;
NSString *password = self.password.text;
[[XMPPManager defaultXMPPManager]registerWithUser:userName andPassWord:password];
}
#pragma mark -XMPPStreamDelegate
//验证注册成功
-(void)xmppStreamDidRegister:(XMPPStream *)sender{
NSLog(@"验证注册成功");
[self.navigationController popToRootViewControllerAnimated:YES];
}
//验证注册失败
-(void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error{
NSLog(@"验证注册失败= %@",error);
}
@end
#import <Foundation/Foundation.h>
#import "XMPPFramework.h"
@interface XMPPManager : NSObject<XMPPStreamDelegate]] >
//声明通信管道 所有客户端和服务器连接都是通过stream对象来完成连接的
@property(nonatomic,strong)XMPPStream *xmppStream;
//添加属性(XMPP 提供的好友花名册,所有的好友信息都是通过它来进行管理)
@property(nonatomic,strong)XMPPRoster *xmppRoster;
//添加信息归档的一个类
@property(nonatomic,strong)XMPPMessageArchiving *xmppMessageArchiving;
//创建上下文的属性(信息归档的上下文)
@property(nonatomic,strong)NSManagedObjectContext *context;
+(instancetype)defaultXMPPManager;
//添加登录的方法
-(void)loginWithUser:(NSString *)user andPassWord:(NSString *)password;
//注册的方法
-(void)registerWithUser:(NSString *)user andPassWord:(NSString *)password;
@end
#import "XMPPFramework.h"
@interface XMPPManager : NSObject<XMPPStreamDelegate]] >
//声明通信管道 所有客户端和服务器连接都是通过stream对象来完成连接的
@property(nonatomic,strong)XMPPStream *xmppStream;
//添加属性(XMPP 提供的好友花名册,所有的好友信息都是通过它来进行管理)
@property(nonatomic,strong)XMPPRoster *xmppRoster;
//添加信息归档的一个类
@property(nonatomic,strong)XMPPMessageArchiving *xmppMessageArchiving;
//创建上下文的属性(信息归档的上下文)
@property(nonatomic,strong)NSManagedObjectContext *context;
+(instancetype)defaultXMPPManager;
//添加登录的方法
-(void)loginWithUser:(NSString *)user andPassWord:(NSString *)password;
//注册的方法
-(void)registerWithUser:(NSString *)user andPassWord:(NSString *)password;
@end
#import "XMPPManager.h"
//枚举服务器链接类型
typedef NS_ENUM(NSUInteger,ConnectServerType){
connectServerlogin, // 登录连接
connectServerregister,//注册连接
};
@interface XMPPManager ()
//设置登录密码
@property(nonatomic,copy)NSString *loginPassword;
//设置注册密码
@property(nonatomic,copy)NSString *registerPassword;
@property(nonatomic)ConnectServerType connectServerType;
@end
@implementation XMPPManager
//GCD初始化单例
+(instancetype)defaultXMPPManager{
static XMPPManager *XMPP = nil;
static dispatch_once_t once;
dispatch_once(&once,^{
XMPP = [[XMPPManager alloc]init];
});
return XMPP;
}
//初始化stream 对象
- (instancetype)init
{
self = [super init];
if (self) {
//初始化通信管道类
self.xmppStream = [[XMPPStream alloc]init];
//添加服务器ip地址
self.xmppStream.hostName = kHostName;
//添加端口号
self.xmppStream.hostPort = kHostPort;
//添加代理。返回主线程方法中
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//初始化好友花名册对象
self.xmppRoster =[[XMPPRoster alloc]initWithRosterStorage:[XMPPRosterCoreDataStorage sharedInstance] dispatchQueue:dispatch_get_main_queue()];
//激活好友花名册对象
[self.xmppRoster activate:self.xmppStream];
//初始化一个data管理
XMPPMessageArchivingCoreDataStorage *xmppMessageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
//初始化信息归档类
self.xmppMessageArchiving = [[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:xmppMessageStorage dispatchQueue:dispatch_get_main_queue()];
//激活
[self.xmppMessageArchiving activate:self.xmppStream];
//取组线程的上下文
self.context = xmppMessageStorage.mainThreadManagedObjectContext;
}
return self;
}
//连接服务器方法(登录和注册都用到的,进行一个小小的封装)
/////////
-(void)connectToServer:(NSString *)user{
//添加账号对象类中,
XMPPJID *jid = [XMPPJID jidWithUser:user domain:kDomin resource:kResource];
//设置myjid
[self.xmppStream setMyJID:jid];
//判断当前管道是否连接,如果是连接状态断开连接
if ([self.xmppStream isConnected]) {
[self.xmppStream disconnect];
}
//连接服务器
[self.xmppStream connectWithTimeout:30.0f error:nil];
}
/////////
//登录
-(void)loginWithUser:(NSString *)user andPassWord:(NSString *)password
{
//调用连接方法
self.loginPassword = password;
self.connectServerType = connectServerlogin;
[self connectToServer:user];
}
//注册
-(void)registerWithUser:(NSString *)user andPassWord:(NSString *)password
{
//调用连接方法
self.registerPassword = password;
self.connectServerType = connectServerregister;
[self connectToServer:user];
}
#pragma XMPPStreamDelegate
//与服务器连接成功
-(void)xmppStreamDidConnect:(XMPPStream *)sender{
NSLog(@"链接成功");
switch (self.connectServerType) {
case connectServerlogin:
//在连接成功密码进行验证
//登录
[self.xmppStream authenticateWithPassword:self.loginPassword error:nil];
break;
case connectServerregister:
//在连接成功密码进行验证
//注册
[self.xmppStream registerWithPassword:self.registerPassword error:nil];
default:
break;
}
}
//与服务器连接失败(在下次做项目的过程中记得一定要进行初五数据的操作,就算错了,也要知道在哪里出错)
-(void)xmppStreamConnectDidTimeout:(XMPPStream *)sender{
NSLog(@"链接失败");
}
@end
//枚举服务器链接类型
typedef NS_ENUM(NSUInteger,ConnectServerType){
connectServerlogin, // 登录连接
connectServerregister,//注册连接
};
@interface XMPPManager ()
//设置登录密码
@property(nonatomic,copy)NSString *loginPassword;
//设置注册密码
@property(nonatomic,copy)NSString *registerPassword;
@property(nonatomic)ConnectServerType connectServerType;
@end
@implementation XMPPManager
//GCD初始化单例
+(instancetype)defaultXMPPManager{
static XMPPManager *XMPP = nil;
static dispatch_once_t once;
dispatch_once(&once,^{
XMPP = [[XMPPManager alloc]init];
});
return XMPP;
}
//初始化stream 对象
- (instancetype)init
{
self = [super init];
if (self) {
//初始化通信管道类
self.xmppStream = [[XMPPStream alloc]init];
//添加服务器ip地址
self.xmppStream.hostName = kHostName;
//添加端口号
self.xmppStream.hostPort = kHostPort;
//添加代理。返回主线程方法中
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
//初始化好友花名册对象
self.xmppRoster =[[XMPPRoster alloc]initWithRosterStorage:[XMPPRosterCoreDataStorage sharedInstance] dispatchQueue:dispatch_get_main_queue()];
//激活好友花名册对象
[self.xmppRoster activate:self.xmppStream];
//初始化一个data管理
XMPPMessageArchivingCoreDataStorage *xmppMessageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
//初始化信息归档类
self.xmppMessageArchiving = [[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:xmppMessageStorage dispatchQueue:dispatch_get_main_queue()];
//激活
[self.xmppMessageArchiving activate:self.xmppStream];
//取组线程的上下文
self.context = xmppMessageStorage.mainThreadManagedObjectContext;
}
return self;
}
//连接服务器方法(登录和注册都用到的,进行一个小小的封装)
/////////
-(void)connectToServer:(NSString *)user{
//添加账号对象类中,
XMPPJID *jid = [XMPPJID jidWithUser:user domain:kDomin resource:kResource];
//设置myjid
[self.xmppStream setMyJID:jid];
//判断当前管道是否连接,如果是连接状态断开连接
if ([self.xmppStream isConnected]) {
[self.xmppStream disconnect];
}
//连接服务器
[self.xmppStream connectWithTimeout:30.0f error:nil];
}
/////////
//登录
-(void)loginWithUser:(NSString *)user andPassWord:(NSString *)password
{
//调用连接方法
self.loginPassword = password;
self.connectServerType = connectServerlogin;
[self connectToServer:user];
}
//注册
-(void)registerWithUser:(NSString *)user andPassWord:(NSString *)password
{
//调用连接方法
self.registerPassword = password;
self.connectServerType = connectServerregister;
[self connectToServer:user];
}
#pragma XMPPStreamDelegate
//与服务器连接成功
-(void)xmppStreamDidConnect:(XMPPStream *)sender{
NSLog(@"链接成功");
switch (self.connectServerType) {
case connectServerlogin:
//在连接成功密码进行验证
//登录
[self.xmppStream authenticateWithPassword:self.loginPassword error:nil];
break;
case connectServerregister:
//在连接成功密码进行验证
//注册
[self.xmppStream registerWithPassword:self.registerPassword error:nil];
default:
break;
}
}
//与服务器连接失败(在下次做项目的过程中记得一定要进行初五数据的操作,就算错了,也要知道在哪里出错)
-(void)xmppStreamConnectDidTimeout:(XMPPStream *)sender{
NSLog(@"链接失败");
}
@end
1 0
- ios开发-XMPP功能的实现
- ios xmpp openfire实现聊天功能
- IOS开发,XMPP实现聊天,即时通讯(一)
- IOS开发,XMPP实现聊天,即时通讯(二)
- IOS开发,XMPP实现聊天,即时通讯(三)
- XMPP实现简单的登录注册功能
- 基于XMPP的IOS实现单聊,群聊功能(一)
- 基于XMPP的IOS实现单聊,群聊功能(二)
- 基于XMPP协议的IOS实现(二)XMPP解析
- iOS开发之摇一摇功能的实现
- iOS开发之摇一摇功能的实现
- iOS开发之摇一摇功能的实现
- iOS开发之摇一摇功能的实现
- iOS开发-清理缓存功能的实现
- iOS开发-清理缓存功能的实现
- iOS开发-清理缓存功能的实现
- iOS开发-清理缓存功能的实现
- iOS开发之摇一摇功能的实现
- 汇编语言-端口和外中断
- Struts2 URL参数 s:if 判断运用
- 链接创建按钮
- MySQL的命令行预备
- jsp学习笔记
- ios开发-XMPP功能的实现
- hdoj5240Exam
- C#酒店管理系统(内部)业务逻辑
- ios开发-AVPlayer 音乐播放自定义经典例题
- 自己编译操作系统,安装systemtap
- java parseint 和 Integer.value
- 1369 - Answering Queries
- 约分差束1
- ios开发-UISlider通过滑动添加单元格功能