1. 环境准备:openfire +MySQL5.1.6 + Xcode5 + XMPP.Framework (至于环境的配置, 请自己百度, 推荐: http://www.cnblogs.com/xiaodao/archive/2013/04/05/3000554.html)
2. cocoapods
1platform :ios,'7.0'2pod"XMPPFramework","~>3.6.4"
3.工程: ARC + COREDATA + XIB(Storyboard)
4.直奔重点: XMPP工具单例
1. 首先, 创建一个Infromation.h 文件 用来存放我们需要的一些基本信息,比如host之类的。 然后把这个头文件导入pch文件中 注意我的Domains 后边多一个@
1#ifndef ADXMPP_BE_Information_h2#defineADXMPP_BE_Information_h34#defineSERVER @"127.0.0.1"5#defineDOMAINS @"@127.0.0.1"67#endif
2. 创建ADXMPPConn 类、 开始编辑我们的XMPP连接、 登录、 注册核心代码
ADXMPPConn.h
1//2//ADXMPPConn.h3//ADXMPP_BE4//5//Created by Dylan on 14-10-8.6//Copyright (c) 2014年 Dylan. All rights reserved.7//89#import<Foundation/Foundation.h>10#import<XMPP.h>1112/**13用来判断当前用户在进行什么操作14*/15typedefenum{16LOGIN,17REGISTER18}USER_TYPE;1920/*!21* @Author Dylan.22*23* Callbacl Block24*/25typedefvoid(^connectSuccess)();26typedefvoid(^AuthenticateFailure)(id);2728typedefvoid(^registerSuccess)();29typedefvoid(^registerFailure)(id);3031@interfaceADXMPPConn : NSObject <XMPPStreamDelegate>3233/*!34* @Author Dylan.35*36* xmppStream37*/38@PRoperty (nonatomic, strong) XMPPStream *xmppStream;3940/*!41* @Author Dylan.42*43* Username, PassWord44*/45@property (nonatomic, strong) NSString *userName;46@property (nonatomic, strong) NSString *passWord;4748/*!49* @Author Dylan. UserType50*/51@property (nonatomic) USER_TYPE USERTYPE;5253/*!54* @Author Dylan.55*56* Methods57*/58#pragmamark - Methods5960/*!61* shareInstance62*/63+(instancetype)shareInstance;6465/*!66* setup xmppStream67*/68- (void) setupXmppStream;6970/*!71* on/off line72*/73- (void) online;74- (void) offline;7576/*!77* connection/register78*/79- (BOOL)connectionWithUserName: (NSString *)userName80passWord: (NSString *)passWord81success: (connectSuccess)Success82failure: (AuthenticateFailure)Failure;8384- (void)registerWithUserName: (NSString *)userName85passWord: (NSString *)passWord86success: (registerSuccess)Success87failure: (registerFailure)Failure;8889@end
ADXMPPConn.m
1//2//ADXMPPConn.m3//ADXMPP_BE4//5//Created by Dylan on 14-10-8.6//Copyright (c) 2014年 Dylan. All rights reserved.7//89#import"ADXMPPConn.h"1011@interfaceADXMPPConn ()1213/*!14* @Author Dylan.15*16* Callback Block17*/18@property (nonatomic, copy) connectSuccess connSuccess;19@property (nonatomic, copy) AuthenticateFailure authenFailure;2021@property (nonatomic, copy) registerSuccess regisSuccess;22@property (nonatomic, copy) registerFailure regisFailure;2324@end2526//shareInstance27staticADXMPPConn *xmppConn;2829@implementationADXMPPConn3031#pragmamark shareInstance32+(instancetype)shareInstance {33staticdispatch_once_t onceToken;34dispatch_once(&onceToken, ^{35xmppConn =[[self alloc] init];36});3738returnxmppConn;39}4041#pragmamark - Methods42- (void)setupXmppStream {43self.xmppStream =[[XMPPStream alloc] init];44[_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];45}4647#pragmamark on/off line48- (void)online {49XMPPPresence * presence =[XMPPPresence presence];50[self.xmppStream sendElement:presence];51}5253- (void)offline {54XMPPPresence * presence = [XMPPPresence presenceWithType:@"unavailable"];55[self.xmppStream sendElement:presence];56[self.xmppStream disconnect];57}5859#pragmamark connection60- (BOOL)connectionWithUserName:(NSString *)userName passWord:(NSString *)passWord success:(connectSuccess)Success failure:(AuthenticateFailure)Failure {6162//setup xmppStream63[self setupXmppStream];6465//get username, password66self.userName =userName;67self.passWord =passWord;6869//set callback block70self.connSuccess =Success;71self.authenFailure =Failure;7273if([self.xmppStream isConnected]) {74returnYES;75}7677if(userName ==nil) {78returnNO;79}8081//setJID82[self.xmppStream setMyJID:[XMPPJID jidWithString:userName]];83[self.xmppStream setHostName:SERVER];8485NSError * error =nil;86if(![self.xmppStream connectWithTimeout:30error:&error]) {87NSLog(@"%@", [error localizedDescription]);88Failure(error);89returnNO;90}9192returnYES;93}9495- (void)registerWithUserName:(NSString *)userName passWord:(NSString *)passWord success:(registerSuccess)Success failure:(registerFailure)Failure {9697//set user type98self.USERTYPE =REGISTER;99100//set username, password101self.userName =[userName stringByAppendingString:DOMAINS];102self.passWord =passWord;103104self.regisSuccess =Success;105self.regisFailure =Failure;106107[self connectionWithUserName:self.userName passWord:passWord success:Success failure:Failure];108}109110#pragmamark - delegateMethods111- (void)xmppStreamDidConnect:(XMPPStream *)sender {113114NSError * error =nil;115116//kind of user type117if(self.USERTYPE ==REGISTER) {118119//registe120[self.xmppStream setMyJID:[XMPPJID jidWithString:self.userName]];121NSError * error =nil;122if(![self.xmppStream registerWithPassword:self.passWord error:&error]) {123self.regisFailure([error localizedDescription]);124}125126}else{127//authenticate128[self.xmppStream authenticateWithPassword:self.passWord error:&error];129if(error !=nil) {130self.authenFailure([error localizedDescription]);131}132}133}134135//dis connect136- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error {137NSLog(@"%@", [error localizedDescription]);138}139140//authenticate141- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDxmlElement *)error {142self.authenFailure(error);143}144145- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {146//online147[self online];148self.connSuccess();149}150151//regist152- (void)xmppStreamDidRegister:(XMPPStream *)sender {153self.regisSuccess();154}155156- (void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error {157self.regisFailure(error);158}159160@end
登录注册。 每日更新中。