大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > XMPP登录注册1

XMPP登录注册1

关键词:登录注册XMPP  阅读(725) 赞(11)

[摘要]本文是对XMPP登录注册1的讲解,对学习IOS苹果软件开发有所帮助,与大家分享。

1. 环境准备:openfire + mysql 5.1.6 + Xcode5 + XMPP.Framework (至于环境的配置, 请自己百度, 推荐:http://www.cnblogs.com/xiaodao/archive/2013/04/05/3000554.html)

2. cocoapods

 platform :ios, '7.0'
 pod "XMPPFramework", "~>3.6.4"

3.工程: ARC + COREDATA + XIB(Storyboard)

4.直奔重点: XMPP工具单例

  1. 首先, 创建一个Infromation.h 文件 用来存放我们需要的一些基本信息,比如host之类的。 然后把这个头文件导入pch文件中 注意我的Domains 后边多一个@

 #ifndef ADXMPP_BE_Information_h
 #define ADXMPP_BE_Information_h
 
 #define SERVER @"127.0.0.1"
 #define DOMAINS @"@127.0.0.1"
 
 #endif

  2. 创建ADXMPPConn 类、 开始编辑我们的XMPP连接、 登录、 注册核心代码

ADXMPPConn.h

 //
 //  ADXMPPConn.h
 //  ADXMPP_BE
 //
 //  Created by Dylan on 14-10-8.
 //  Copyright (c) 2014年 Dylan. All rights reserved.
 //
 
 #import <Foundation/Foundation.h>
 #import <XMPP.h>
 
 /**
  用来判断当前用户在进行什么操作
  */
 typedef enum {
     LOGIN,
     REGISTER
 }USER_TYPE;
 
 /*!
  *  @Author Dylan.
  *
  *  Callbacl Block
  */
 typedef void(^connectSuccess)();
 typedef void(^AuthenticateFailure)(id);
 
 typedef void(^registerSuccess)();
 typedef void(^registerFailure)(id);
 
 @interface ADXMPPConn : NSObject <XMPPStreamDelegate>
 
 /*!
  *  @Author Dylan.
  *
  *  xmppStream
  */
 @property (nonatomic, strong) XMPPStream * xmppStream;
 
 /*!
  *  @Author Dylan.
  *
  *  Username, Password
  */
 @property (nonatomic, strong) NSString * userName;
 @property (nonatomic, strong) NSString * passWord;
 
 /*!
  *  @Author Dylan. UserType
  */
 @property (nonatomic) USER_TYPE USERTYPE;
 
 /*!
  *  @Author Dylan.
  *
  *  Methods
  */
 #pragma mark - Methods
 
 /*!
  *  shareInstance
  */
 + (instancetype)shareInstance;
 
 /*!
  *  setup xmppStream
  */
 - (void) setupXmppStream;
 
 /*!
  *  on/off line
  */
 - (void) online;
 - (void) offline;
 
 /*!
  *  connection/register
  */
 - (BOOL)connectionWithUserName: (NSString *)userName
                       passWord: (NSString *)passWord
                        success: (connectSuccess)Success
                        failure: (AuthenticateFailure)Failure;
 
 - (void)registerWithUserName: (NSString *)userName
                     passWord: (NSString *)passWord
                      success: (registerSuccess)Success
                      failure: (registerFailure)Failure;
 
 @end

ADXMPPConn.m

 //
 //  ADXMPPConn.m
 //  ADXMPP_BE
 //
 //  Created by Dylan on 14-10-8.
 //  Copyright (c) 2014年 Dylan. All rights reserved.
 //
 
 #import "ADXMPPConn.h"
 
 @interface ADXMPPConn ()
 
 /*!
  *  @Author Dylan.
  *
  *  Callback Block
  */
 @property (nonatomic, copy) connectSuccess connSuccess;
 @property (nonatomic, copy) AuthenticateFailure authenFailure;
 
 @property (nonatomic, copy) registerSuccess regisSuccess;
 @property (nonatomic, copy) registerFailure regisFailure;
 
 @end
 
 // shareInstance
 static ADXMPPConn * xmppConn;
 
 @implementation ADXMPPConn
 
 #pragma mark shareInstance
 + (instancetype)shareInstance {
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         xmppConn = [[self alloc] init];
     });
     
     return xmppConn;
 }
 
 #pragma mark - Methods
 - (void)setupXmppStream {
     self.xmppStream = [[XMPPStream alloc] init];
     [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
 }
 
 #pragma mark on/off line
 - (void)online {
     XMPPPresence * presence = [XMPPPresence presence];
     [self.xmppStream sendElement:presence];
 }
 
 - (void)offline {
     XMPPPresence * presence = [XMPPPresence presenceWithType:@"unavailable"];
     [self.xmppStream sendElement:presence];
     [self.xmppStream disconnect];
 }
 
 #pragma mark connection
 - (BOOL)connectionWithUserName:(NSString *)userName passWord:(NSString *)passWord success:(connectSuccess)Success failure:(AuthenticateFailure)Failure {
     
     // setup xmppStream
     [self setupXmppStream];
     
     // get username, password
     self.userName = userName;
     self.passWord = passWord;
     
     // set callback block
     self.connSuccess = Success;
     self.authenFailure = Failure;
     
     if ([self.xmppStream isConnected]) {
         return YES;
     }
     
     if (userName == nil) {
         return NO;
     }
     
     // setJID
     [self.xmppStream setMyJID:[XMPPJID jidWithString:userName]];
     [self.xmppStream setHostName:SERVER];
     
     NSError * error = nil;
     if (![self.xmppStream connectWithTimeout:30 error:&error]) {
         NSLog(@"%@", [error localizedDescription]);
         Failure(error);
         return NO;
     }
     
     return YES;
 }
 
 - (void)registerWithUserName:(NSString *)userName passWord:(NSString *)passWord success:(registerSuccess)Success failure:(registerFailure)Failure {
     
     // set user type
     self.USERTYPE = REGISTER;
     
     // set username, password
     self.userName = [userName stringByAppendingString:DOMAINS];
     self.passWord = passWord;
     
     self.regisSuccess = Success;
     self.regisFailure = Failure;
     
     [self connectionWithUserName:self.userName passWord:passWord success:Success failure:Failure];
 }
 
 #pragma mark - delegateMethods
 - (void)xmppStreamDidConnect:(XMPPStream *)sender {     
     NSError * error = nil;
     
     // kind of user type
     if (self.USERTYPE == REGISTER) {
         
         // registe
         [self.xmppStream setMyJID:[XMPPJID jidWithString:self.userName]];
         NSError * error = nil;
         if (![self.xmppStream registerWithPassword:self.passWord error:&error]) {
             self.regisFailure([error localizedDescription]);
         }
         
     } else {
         // authenticate
         [self.xmppStream authenticateWithPassword:self.passWord error:&error];
         if (error != nil) {
             self.authenFailure([error localizedDescription]);
         }
     }
 }
 
 // dis connect
 - (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error {
     NSLog(@"%@", [error localizedDescription]);
 }
 
 // authenticate
 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error {
     self.authenFailure(error);
 }
 
 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
     // online
     [self online];
     self.connSuccess();
 }
 
 // regist
 - (void)xmppStreamDidRegister:(XMPPStream *)sender {
     self.regisSuccess();
 }
 
 - (void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error {
     self.regisFailure(error);
 }
 
 @end

登录注册。 每日更新中。



相关评论