
// Roster
我们继续写 获取好友列表
.h
1 /*! 2 * @Author Dylan. 3 * 4 * Roster 5 */ 6 7 typedef void (^refreshRosterListFailure) (id); 8 typedef void (^Rosterlist) (id); 9 10 /*! 11 * @Author Dylan. 12 * 13 * request for roster list. IQ 14 */ 15 - (void)refreshRosterList: (Rosterlist)success 16 failure: (refreshRosterListFailure)failure; 17 @PRoperty (nonatomic, strong) NSMutableDictionary * rosterDict;
.m
1 #pragma mark - rosterList
2
3 - (void)initRosterlist {
4 self.rosterDict = [NSMutableDictionary dictionary];
5 }
6
7 - (void)refreshRosterList: (Rosterlist)success
8 failure: (refreshRosterListFailure)failure {
9
10 // call back
11 self.refreshSuccess = success;
12 self.refreshFailure = failure;
13
14 NSxmlElement * query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
15 NSXMLElement * iq = [NSXMLElement elementWithName:@"iq"];
16
17 XMPPJID * myJID = self.xmppStream.myJID;
18 [iq addAttributeWithName:@"from" stringValue:myJID.description];
19 [iq addAttributeWithName:@"to" stringValue:myJID.domain];
20 [iq addAttributeWithName:@"id" stringValue:@"123456"];
21 [iq addAttributeWithName:@"type" stringValue:@"get"];
22 [iq addChild:query];
23
24 [self.xmppStream sendElement:iq];
25 }
26
27 - (void)xmppStream:(XMPPStream *)sender didFailToSendIQ:(XMPPIQ *)iq error:(NSError *)error {
28 self.refreshFailure(error);
29 }
30
31 // get user list
32 - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
33
34 // kind of result
35 if ([@"result" isEqualToString:iq.type]) {
36 NSXMLElement * query = iq.childElement;
37
38 if ([@"query" isEqualToString:query.name]) {
39 NSArray * items = [query children];
40 for (NSXMLElement * item in items) {
41 NSString * jid = [item attributeStringValueForName:@"jid"];
42 XMPPJID * xmppJID = [XMPPJID jidWithString:jid];
43 [_rosterDict setValue:xmppJID forKey:jid];
44 }
45 }
46 // block
47 self.refreshSuccess(_rosterDict);
48 return YES;
49 }
50
51 NSLog(@"get iq error");
52 return NO;
53 }
54
55
56 @end
// 顺便写出在点m文件中我写的回掉Block 的属性
1 @interface ADXMPPConn () 2 3 /*! 4 * @Author Dylan. 5 * 6 * Call back Block 7 */ 8 @property (nonatomic, copy) connectSuccess connSuccess; 9 @property (nonatomic, copy) AuthenticateFailure authenFailure; 10 11 @property (nonatomic, copy) registerSuccess regisSuccess; 12 @property (nonatomic, copy) registerFailure regisFailure; 13 14 /*! 15 * call back block 16 */ 17 @property (nonatomic, copy) sendSuccess success; 18 @property (nonatomic, copy) sendFailure failure; 19 20 /*! 21 * call back block 22 */ 23 @property (nonatomic, copy) refreshRosterListFailure refreshFailure; 24 @property (nonatomic, copy) Rosterlist refreshSuccess; 25 26 @end