// Roster
我們繼續寫 獲取好友列表
.h
/*!
* @Author Dylan.
*
* Roster
*/
typedef void (^refreshRosterListFailure) (id);
typedef void (^Rosterlist) (id);
/*!
* @Author Dylan.
*
* request for roster list. IQ
*/
- (void)refreshRosterList: (Rosterlist)success
failure: (refreshRosterListFailure)failure;
@property (nonatomic, strong) NSMutableDictionary * rosterDict;
.m
#pragma mark - rosterList
- (void)initRosterlist {
self.rosterDict = [NSMutableDictionary dictionary];
}
- (void)refreshRosterList: (Rosterlist)success
failure: (refreshRosterListFailure)failure {
// call back
self.refreshSuccess = success;
self.refreshFailure = failure;
NSXMLElement * query = [NSXMLElement elementWithName:@"query" xmlns:@"jabber:iq:roster"];
NSXMLElement * iq = [NSXMLElement elementWithName:@"iq"];
XMPPJID * myJID = self.xmppStream.myJID;
[iq addAttributeWithName:@"from" stringValue:myJID.description];
[iq addAttributeWithName:@"to" stringValue:myJID.domain];
[iq addAttributeWithName:@"id" stringValue:@"123456"];
[iq addAttributeWithName:@"type" stringValue:@"get"];
[iq addChild:query];
[self.xmppStream sendElement:iq];
}
- (void)xmppStream:(XMPPStream *)sender didFailToSendIQ:(XMPPIQ *)iq error:(NSError *)error {
self.refreshFailure(error);
}
// get user list
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
// kind of result
if ([@"result" isEqualToString:iq.type]) {
NSXMLElement * query = iq.childElement;
if ([@"query" isEqualToString:query.name]) {
NSArray * items = [query children];
for (NSXMLElement * item in items) {
NSString * jid = [item attributeStringValueForName:@"jid"];
XMPPJID * xmppJID = [XMPPJID jidWithString:jid];
[_rosterDict setValue:xmppJID forKey:jid];
}
}
// block
self.refreshSuccess(_rosterDict);
return YES;
}
NSLog(@"get iq error");
return NO;
}
@end
// 順便寫出在點m文件中我寫的回掉Block 的屬性
@interface ADXMPPConn () /*! * @Author Dylan. * * Call back Block */ @property (nonatomic, copy) connectSuccess connSuccess; @property (nonatomic, copy) AuthenticateFailure authenFailure; @property (nonatomic, copy) registerSuccess regisSuccess; @property (nonatomic, copy) registerFailure regisFailure; /*! * call back block */ @property (nonatomic, copy) sendSuccess success; @property (nonatomic, copy) sendFailure failure; /*! * call back block */ @property (nonatomic, copy) refreshRosterListFailure refreshFailure; @property (nonatomic, copy) Rosterlist refreshSuccess; @end