大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > xmpp好友请求5

xmpp好友请求5

关键词:请求好友xmpp  阅读(819) 赞(19)

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

实现好友请求

.m

 #pragma mark - rosterHandle
 
 // initRoster
 - (void)initRoster {
     self.xmppRosterMemory = [[XMPPRosterMemoryStorage alloc] init];
     self.xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:self.xmppRosterMemory];
     [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
     [_xmppRoster activate:self.xmppStream];
 }
 
 - (void)addFriend: (NSString *)accountName {
     [_xmppRoster addUser:[XMPPJID jidWithString:[accountName stringByAppendingString:DOMAINS]] withNickname:nil];
 }
 
 - (void)removeFriend: (NSString *)accountName {
     [_xmppRoster removeUser:[XMPPJID jidWithString:[accountName stringByAppendingString:DOMAINS]]];
 }
 
 // call back
 - (void)dealWithFriendAsk: (BOOL)isAgree
               accountName: (NSString *)accountName {
     XMPPJID * jid=[XMPPJID jidWithString:[NSString stringWithFormat:@"%@%@",accountName,DOMAINS]];
     if(isAgree){
         [self.xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:NO];
     }else{
         [self.xmppRoster rejectPresenceSubscriptionRequestFrom:jid];
     }
 
 }
 
 #pragma mark addFriendDelegateMethods
 - (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence {
     
     NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]];
     if (self.acceptOrDenyFriend != nil) {
         BOOL isAgree = self.acceptOrDenyFriend(presenceFromUser);
         [self dealWithFriendAsk:isAgree accountName:presenceFromUser];
     }
 }
 
 @end

.h

 /*!
  *  @Author Dylan.
  *
  *  addRoster.
  */
 // if you want to deny or add friend. please call this block
 @property (nonatomic, copy) BOOL (^acceptOrDenyFriend) (NSString *);
 @property (nonatomic, strong) XMPPRoster * xmppRoster;
 
 /*!
  *  @Author Dylan. Methods
  */
 - (void)addFriend: (NSString *)accountName;
 - (void)removeFriend: (NSString *)accountName;


相关评论