XMPPFramework-IOS开发(二)登陆以及自定义类

XMPPFramework自带的例子,很不适合正式的应用环境,所以在登陆之前,我们先把xmpp相关操作提取出来,写成一个单独的类来进行管理操作

 

先介绍下XMPPFramework里面常用的类

 

XMPPStream:xmpp基础服务类

XMPPRoster:好友列表类

XMPPRosterCoreDataStorage:好友列表(用户账号)在core data中的操作类

XMPPvCardCoreDataStorage:好友名片(昵称,签名,性别,年龄等信息)在core data中的操作类

XMPPvCardTemp:好友名片实体类,从数据库里取出来的都是它

XMPPvCardAvatarModule:好友头像

XMPPReconnect:如果失去连接,自动重连

XMPPRoom:提供多用户聊天支持

XMPPPubSub:发布订阅



 

1、新建一个XMPPManager类

 

2、引入头文件

 1 #import "XMPPFramework.h" 


3、定义下所需属性和方法

 1 @property (nonatomic, strong, readonly) XMPPStream *xmppStream;
 2  
 3 //xmpp自动连接对象,在异常断开后可以尝试自动连接
 4 @property (nonatomic, strong, readonly) XMPPReconnect *xmppReconnect;
 5  
 6 //xmpp中好友列表
 7 @property (nonatomic, strong, readonly) XMPPRoster *xmppRoster;
 8  
 9 //初始化Stream
10 - (void)setupStream;
11  
12 //连接服务器
13 - (BOOL)connect;
14  
15 //断开服务器
16 - (void)disconnect;
17  
18 //登出
19 - (void)logout;
20  
21 //下线,此处下线是指发消息给服务器,变更下自己的状态,而不是断开socket连接
22 - (void)goOffline;
23  
24 //获取用户JID
25 - (XMPPJID *) getMyJid;

 

 

4、方法实现,直接帖代码

  1 #import "XMPPManager.h"
  2  
  3 @interface XMPPManager()
  4 @property (nonatomic, strong) NSString *password;
  5 @property (nonatomic, assign) BOOL isXmppConnected;
  6 @property (nonatomic, assign) BOOL isXmppSetuped;
  7 @property (nonatomic, assign) BOOL allowSelfSignedCertificates;
  8 @property (nonatomic, assign) BOOL allowSSLHostNameMismatch;
  9  
 10  
 11 - (void)goOnline;
 12 - (void)goOffline;
 13 @end
 14  
 15 @implementation XMPPManager
 16  
 17 - (void)setupStream {
 18     NSLog(@"xmpp setup");
 19     if (self.isXmppSetuped) {
 20         return;
 21     }
 22      
 23     _xmppStream = [[XMPPStream alloc] init];
 24     _xmppReconnect = [[XMPPReconnect alloc] init];
 25  
 26     // Activate xmpp modules
 27     [_xmppReconnect activate:_xmppStream];
 28     [_xmppReconnect setAutoReconnect:YES];
 29      
 30      
 31     // Add ourself as a delegate to anything we may be interested in
 32     [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
 33     [_xmppReconnect addDelegate:self delegateQueue:dispatch_get_main_queue()];
 34      
 35      
 36     [_xmppStream setHostName:@"192.168.1.209"];
 37      
 38      
 39     [_xmppStream setHostPort:5222];
 40      
 41     // You may need to alter these settings depending on the server you're connecting to
 42      
 43     self.allowSelfSignedCertificates = NO;
 44     self.allowSSLHostNameMismatch = NO;
 45      
 46      
 47 }
 48  
 49 -(XMPPJID *) getMyJid
 50 {
 51     return [self.xmppStream myJID];
 52 }
 53  
 54 - (void)goOnline {
 55     NSLog(@"xmpp goOnline");
 56      
 57     // 上传自己的在线状态
 58     NSString *status=[[NSUserDefaults standardUserDefaults] stringForKey:@"status"];
 59     if (!status) {
 60         status=@"available";
 61     }
 62     XMPPPresence *presence = [XMPPPresence presenceWithType:status];
 63     [[self xmppStream] sendElement:presence];
 64      
 65 }
 66  
 67 //下线
 68 - (void)goOffline {
 69     XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
 70     [[self xmppStream] sendElement:presence];
 71 }
 72  
 73 - (BOOL)connect {
 74     //[self setupStream];
 75     NSLog(@"xmpp connect");
 76      
 77     NSString *jabberID = [[NSUserDefaults standardUserDefaults] objectForKey:@"jid"];
 78     NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
 79      
 80     if (![self.xmppStream isDisconnected]) {
 81         return YES;
 82     }
 83      
 84     if (jabberID == nil || myPassword == nil) {
 85         return NO;
 86     }
 87      
 88     [self.xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
 89     self.password = myPassword;
 90     NSError *error = nil;
 91      
 92     if (![self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
 93     {
 94         NSLog(@"Error connecting: %@", error);
 95          
 96         return NO;
 97     }
 98     return YES;
 99 }
100  
101 //登出
102 - (void)logout{
103     [self disconnect];
104     [self teardownStream];
105 }
106  
107 - (void)disconnect {
108      
109     NSLog(@"xmpp disconnect");
110      
111     [self goOffline];
112     [_xmppStream disconnect];
113 }
114  
115 - (void)teardownStream
116 {
117     [_xmppStream removeDelegate:self];
118      
119     [_xmppReconnect deactivate];
120      
121     [_xmppStream disconnect];
122      
123     _xmppStream = nil;
124     _xmppReconnect = nil;
125  
126     self.isXmppSetuped = NO;
127     self.isXmppConnected = NO;
128     self.isAccessibilityElement = NO;
129      
130 }
131  
132 - (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
133 {
134     NSLog(@"socket connect");
135 }
136  
137 - (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
138 {
139      
140     if (self.allowSelfSignedCertificates)
141     {
142         [settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
143     }
144      
145     if (self.allowSSLHostNameMismatch)
146     {
147         [settings setObject:[NSNull null] forKey:(NSString *)kCFStreamSSLPeerName];
148     }
149     else
150     {
151         NSString *expectedCertName = nil;
152          
153         NSString *serverDomain = _xmppStream.hostName;
154         NSString *virtualDomain = [_xmppStream.myJID domain];
155          
156         if (serverDomain == nil)
157         {
158             expectedCertName = virtualDomain;
159         }
160         else
161         {
162             expectedCertName = serverDomain;
163         }
164          
165         if (expectedCertName)
166         {
167             [settings setObject:expectedCertName forKey:(NSString *)kCFStreamSSLPeerName];
168         }
169     }
170 }
171  
172 - (void)xmppStreamDidConnect:(XMPPStream *)sender {
173     NSLog(@"xmpp xmppStreamDidConnect");
174     NSError *error = nil;
175      
176      
177     [[self xmppStream] authenticateWithPassword:self.password error:&error];
178 }
179  
180 - (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
181 {
182     NSLog(@"xmppStreamDidDisconnect with error:%@",error);
183     //[[self appDelegate] hideHUD];
184 }
185  
186 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
187     NSLog(@"xmpp xmppStreamDidAuthenticate");
188      
189     [self goOnline];
190 }
191  
192 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
193 {
194     NSLog(@"xmpp didNotAuthenticate error--%@",error);
195      
196 }
197  
198 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
199      
200     NSLog(@"Received message: %@",message);
201 }
202  
203 - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
204     NSLog(@"--> %@", presence);
205 }
206  
207 - (void)xmppStream:(XMPPStream *)sender didReceiveError:(id)error
208 {
209     NSLog(@"Did Receive Error!");
210 }
211  
212 - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
213 {
214     NSLog(@"did ReceiveIQ");
215     NSLog(@"ReceiveIQ:%@",iq);
216     return NO;
217 }

 

 

这个类的逻辑图如下:

blob.png

 

5、登陆实现

 1 -(IBAction)btnLogin:(id)sender
 2 {
 3     [[NSUserDefaults standardUserDefaults] setObject:self.txtUserName.text forKey:@"jid"];
 4     [[NSUserDefaults standardUserDefaults] setObject:self.txtPassword.text forKey:@"password"];
 5     [[self appDelegate].xmppManager connect];
 6 }
 7  
 8 - (AppDelegate *)appDelegate
 9 {
10     return (AppDelegate *)[UIApplication sharedApplication].delegate;
11 }

 

 

成功连接服务器,并接收到服务器返回的状态信息

blob.png

 

关于这个框架更深入的了解的图以及demo如下:

XMPPFramwork IOS架构图

Chat.zip

 

posted on 2014-10-16 14:10  Keir Lee  阅读(504)  评论(0编辑  收藏  举报

导航