大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > XMPP好友列表3

XMPP好友列表3

关键词:好友列表XMPP  阅读(1346) 赞(12)

[摘要]本文是对XMPP好友列表3的讲解,对学习IOS苹果软件开发有所帮助,与大家分享。

// 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


相关评论