xmpp开发教程(三)-连接和认证

从这章节开始,我将手把手的操作如何实现聊天里面的一些基本功能,从容易向高难度的过渡,这也是我编写程序的基本方法。

这章我们要研究的是连接和认证两个操作。

首先献上代码

 1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 2 #pragma mark Connect/disconnect
 3 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 4 
 5 - (void)connect {
 6     
 7     if (self.xmppStream == nil) {
 8         self.xmppStream = [[XMPPStream alloc] init];
 9         [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
10         
11         xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
12         //    xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];
13         
14         xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
15         
16         xmppRoster.autoFetchRoster = YES;
17         xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
18         [xmppRoster            activate:xmppStream];
19         [xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
20     }
21     
22     
23     if (![self.xmppStream isConnected]) {
24         
25        NSString *userId=@"admin@helloworldtekimac-mini.local";
26        NSString *pass=@"***********";
27        NSString *server=@"helloworldtekimac-mini.local";
28         
29         //设置用户
30        [xmppStream setMyJID:[XMPPJID jidWithString:userId]];
31         //设置服务器
32        [xmppStream setHostName:server];
33         //密码
34        password = pass;
35         
36         NSError *error = nil;
37         NSTimeInterval timeInter=15;
38         if (![self.xmppStream connectWithTimeout:timeInter error:&error]){
39             NSLog(@"Connect Error: %@", [[error userInfo] description]);
40         }
41     }
42 }

第8,9行是初始化xmppStream对象并且设置delegate,保证后边能回调相应的函数。

第14~19行是初始化xmppRoster对象并且设置存储方式,自动检索数据,delegate,与前边的xmppStream相关联。

第25~39行是连接服务器

userId是登录的用户名,pass是密码,server是服务器域名
connectWithTimeout是最新的连接方法,之前用的是connect,不过已经废弃了。

如果一切顺利,会回调相应的方法
1 - (void)xmppStreamDidConnect:(XMPPStream *)sender {
2    // NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
3     NSError *error = nil;
4     if (![self.xmppStream authenticateWithPassword:password error:&error]) {
5         NSLog(@"Authenticate Error: %@", [[error userInfo] description]);
6     }
7 }

第4行表示回调成功后会验证密码,会进入到下面的方法

 1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 2 #pragma mark Authenticate
 3 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 4 
 5 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
 6     [self goOnline];
 7 }
 8 
 9 -(void)goOnline
10 {
11     XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
12     [self.xmppStream sendElement:presence];
13 }
14 
15 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
16 {
17     NSLog(@"Authenticate Error: %@", error);
18 }
 -(void)goOnline的方法主要是想服务器发送一个上线的通知

我们运行程序,设置断点,跟踪路径,可以根据服务器页面来判断用户是否上线



这就说明登录和验证都是成功的,下章节会介绍如何获取好友列表。
 
原文地址:https://www.cnblogs.com/guchengfengyun/p/4049072.html