zoukankan      html  css  js  c++  java
  • XMPP登录注册1

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

    2. cocoapods 

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

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

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

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

    1 #ifndef ADXMPP_BE_Information_h
    2 #define ADXMPP_BE_Information_h
    3 
    4 #define SERVER @"127.0.0.1"
    5 #define DOMAINS @"@127.0.0.1"
    6 
    7 #endif

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

    ADXMPPConn.h

     1 //
     2 //  ADXMPPConn.h
     3 //  ADXMPP_BE
     4 //
     5 //  Created by Dylan on 14-10-8.
     6 //  Copyright (c) 2014年 Dylan. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import <XMPP.h>
    11 
    12 /**
    13  用来判断当前用户在进行什么操作
    14  */
    15 typedef enum {
    16     LOGIN,
    17     REGISTER
    18 }USER_TYPE;
    19 
    20 /*!
    21  *  @Author Dylan.
    22  *
    23  *  Callbacl Block
    24  */
    25 typedef void(^connectSuccess)();
    26 typedef void(^AuthenticateFailure)(id);
    27 
    28 typedef void(^registerSuccess)();
    29 typedef void(^registerFailure)(id);
    30 
    31 @interface ADXMPPConn : NSObject <XMPPStreamDelegate>
    32 
    33 /*!
    34  *  @Author Dylan.
    35  *
    36  *  xmppStream
    37  */
    38 @property (nonatomic, strong) XMPPStream * xmppStream;
    39 
    40 /*!
    41  *  @Author Dylan.
    42  *
    43  *  Username, Password
    44  */
    45 @property (nonatomic, strong) NSString * userName;
    46 @property (nonatomic, strong) NSString * passWord;
    47 
    48 /*!
    49  *  @Author Dylan. UserType
    50  */
    51 @property (nonatomic) USER_TYPE USERTYPE;
    52 
    53 /*!
    54  *  @Author Dylan.
    55  *
    56  *  Methods
    57  */
    58 #pragma mark - Methods
    59 
    60 /*!
    61  *  shareInstance
    62  */
    63 + (instancetype)shareInstance;
    64 
    65 /*!
    66  *  setup xmppStream
    67  */
    68 - (void) setupXmppStream;
    69 
    70 /*!
    71  *  on/off line
    72  */
    73 - (void) online;
    74 - (void) offline;
    75 
    76 /*!
    77  *  connection/register
    78  */
    79 - (BOOL)connectionWithUserName: (NSString *)userName
    80                       passWord: (NSString *)passWord
    81                        success: (connectSuccess)Success
    82                        failure: (AuthenticateFailure)Failure;
    83 
    84 - (void)registerWithUserName: (NSString *)userName
    85                     passWord: (NSString *)passWord
    86                      success: (registerSuccess)Success
    87                      failure: (registerFailure)Failure;
    88 
    89 @end

    ADXMPPConn.m

      1 //
      2 //  ADXMPPConn.m
      3 //  ADXMPP_BE
      4 //
      5 //  Created by Dylan on 14-10-8.
      6 //  Copyright (c) 2014年 Dylan. All rights reserved.
      7 //
      8 
      9 #import "ADXMPPConn.h"
     10 
     11 @interface ADXMPPConn ()
     12 
     13 /*!
     14  *  @Author Dylan.
     15  *
     16  *  Callback Block
     17  */
     18 @property (nonatomic, copy) connectSuccess connSuccess;
     19 @property (nonatomic, copy) AuthenticateFailure authenFailure;
     20 
     21 @property (nonatomic, copy) registerSuccess regisSuccess;
     22 @property (nonatomic, copy) registerFailure regisFailure;
     23 
     24 @end
     25 
     26 // shareInstance
     27 static ADXMPPConn * xmppConn;
     28 
     29 @implementation ADXMPPConn
     30 
     31 #pragma mark shareInstance
     32 + (instancetype)shareInstance {
     33     static dispatch_once_t onceToken;
     34     dispatch_once(&onceToken, ^{
     35         xmppConn = [[self alloc] init];
     36     });
     37     
     38     return xmppConn;
     39 }
     40 
     41 #pragma mark - Methods
     42 - (void)setupXmppStream {
     43     self.xmppStream = [[XMPPStream alloc] init];
     44     [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
     45 }
     46 
     47 #pragma mark on/off line
     48 - (void)online {
     49     XMPPPresence * presence = [XMPPPresence presence];
     50     [self.xmppStream sendElement:presence];
     51 }
     52 
     53 - (void)offline {
     54     XMPPPresence * presence = [XMPPPresence presenceWithType:@"unavailable"];
     55     [self.xmppStream sendElement:presence];
     56     [self.xmppStream disconnect];
     57 }
     58 
     59 #pragma mark connection
     60 - (BOOL)connectionWithUserName:(NSString *)userName passWord:(NSString *)passWord success:(connectSuccess)Success failure:(AuthenticateFailure)Failure {
     61     
     62     // setup xmppStream
     63     [self setupXmppStream];
     64     
     65     // get username, password
     66     self.userName = userName;
     67     self.passWord = passWord;
     68     
     69     // set callback block
     70     self.connSuccess = Success;
     71     self.authenFailure = Failure;
     72     
     73     if ([self.xmppStream isConnected]) {
     74         return YES;
     75     }
     76     
     77     if (userName == nil) {
     78         return NO;
     79     }
     80     
     81     // setJID
     82     [self.xmppStream setMyJID:[XMPPJID jidWithString:userName]];
     83     [self.xmppStream setHostName:SERVER];
     84     
     85     NSError * error = nil;
     86     if (![self.xmppStream connectWithTimeout:30 error:&error]) {
     87         NSLog(@"%@", [error localizedDescription]);
     88         Failure(error);
     89         return NO;
     90     }
     91     
     92     return YES;
     93 }
     94 
     95 - (void)registerWithUserName:(NSString *)userName passWord:(NSString *)passWord success:(registerSuccess)Success failure:(registerFailure)Failure {
     96     
     97     // set user type
     98     self.USERTYPE = REGISTER;
     99     
    100     // set username, password
    101     self.userName = [userName stringByAppendingString:DOMAINS];
    102     self.passWord = passWord;
    103     
    104     self.regisSuccess = Success;
    105     self.regisFailure = Failure;
    106     
    107     [self connectionWithUserName:self.userName passWord:passWord success:Success failure:Failure];
    108 }
    109 
    110 #pragma mark - delegateMethods
    111 - (void)xmppStreamDidConnect:(XMPPStream *)sender {113     
    114     NSError * error = nil;
    115     
    116     // kind of user type
    117     if (self.USERTYPE == REGISTER) {
    118         
    119         // registe
    120         [self.xmppStream setMyJID:[XMPPJID jidWithString:self.userName]];
    121         NSError * error = nil;
    122         if (![self.xmppStream registerWithPassword:self.passWord error:&error]) {
    123             self.regisFailure([error localizedDescription]);
    124         }
    125         
    126     } else {
    127         // authenticate
    128         [self.xmppStream authenticateWithPassword:self.passWord error:&error];
    129         if (error != nil) {
    130             self.authenFailure([error localizedDescription]);
    131         }
    132     }
    133 }
    134 
    135 // dis connect
    136 - (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error {
    137     NSLog(@"%@", [error localizedDescription]);
    138 }
    139 
    140 // authenticate
    141 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error {
    142     self.authenFailure(error);
    143 }
    144 
    145 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
    146     // online
    147     [self online];
    148     self.connSuccess();
    149 }
    150 
    151 // regist
    152 - (void)xmppStreamDidRegister:(XMPPStream *)sender {
    153     self.regisSuccess();
    154 }
    155 
    156 - (void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error {
    157     self.regisFailure(error);
    158 }
    159 
    160 @end

    登录注册。  每日更新中。

  • 相关阅读:
    0:一种过滤机制的MobileMenuList
    MobileMenuImage
    (转)How To Kill runaway processes After Terminating Concurrent Request
    fnd_profile.value('AFLOG_ENABLED')的取值 和配置文件相关SQL
    供应商 银行 SQL (转自ITPUB)
    重启并发管理器
    定义并发请求时 业务实体值集显示没有值数据
    Oracle EBS环境下查找数据源(OAF篇)
    查看在线EBS用户的相关信息
    转,Oracle中关于处理小数点位数的几个函数,取小数位数,Oracle查询函数
  • 原文地址:https://www.cnblogs.com/Dylan-Alice/p/Dylan_XMPP.html
Copyright © 2011-2022 走看看