你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> XMPP好友列表3

XMPP好友列表3

編輯:IOS技巧綜合
[摘要]本文是對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
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved