Xmpp friend requests 5 and xmpp friend requests

Source: Internet
Author: User

Xmpp friend requests 5 and xmpp friend requests

Implement friend requests

. M

 1 #pragma mark - rosterHandle 2  3 // initRoster 4 - (void)initRoster { 5     self.xmppRosterMemory = [[XMPPRosterMemoryStorage alloc] init]; 6     self.xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:self.xmppRosterMemory]; 7     [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()]; 8     [_xmppRoster activate:self.xmppStream]; 9 }10 11 - (void)addFriend: (NSString *)accountName {12     [_xmppRoster addUser:[XMPPJID jidWithString:[accountName stringByAppendingString:DOMAINS]] withNickname:nil];13 }14 15 - (void)removeFriend: (NSString *)accountName {16     [_xmppRoster removeUser:[XMPPJID jidWithString:[accountName stringByAppendingString:DOMAINS]]];17 }18 19 // call back20 - (void)dealWithFriendAsk: (BOOL)isAgree21               accountName: (NSString *)accountName {22     XMPPJID * jid=[XMPPJID jidWithString:[NSString stringWithFormat:@"%@%@",accountName,DOMAINS]];23     if(isAgree){24         [self.xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:NO];25     }else{26         [self.xmppRoster rejectPresenceSubscriptionRequestFrom:jid];27     }28 29 }30 31 #pragma mark addFriendDelegateMethods32 - (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence {33     34     NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]];35     if (self.acceptOrDenyFriend != nil) {36         BOOL isAgree = self.acceptOrDenyFriend(presenceFromUser);37         [self dealWithFriendAsk:isAgree accountName:presenceFromUser];38     }39 }40 41 @end

. H

 1 /*! 2  *  @Author Dylan. 3  * 4  *  addRoster. 5  */ 6 // if you want to deny or add friend. please call this block 7 @property (nonatomic, copy) BOOL (^acceptOrDenyFriend) (NSString *); 8 @property (nonatomic, strong) XMPPRoster * xmppRoster; 9 10 /*!11  *  @Author Dylan. Methods12  */13 - (void)addFriend: (NSString *)accountName;14 - (void)removeFriend: (NSString *)accountName;

 


How does my client handle this when I add an XMPP friend to me? Answer

After xmppStream is created, xmpp framework creates an xmppRoster object for you. This object is used to add or reject friend requests. Method for adding a friend:-(void) addBuddy :( XMPPJID *) jid withNickname :( NSString *) optionalName groupName :( NSString *) groupName where jid is the jid requested to add as a friend. optionalName is the nickname of a friend (displayed in the list ). GroupName is the group in which the requested friend belongs. If it does not exist, xmpp framework will help you create a new one. Consent to and rejection of friend requests. The method used is as follows:-(void) acceptBuddyRequest :( XMPPJID *) jid-(void) rejectBuddyRequest :( XMPPJID *) jid, where jid is the jid of the user applying for adding a friend.
When a friend request is received, the following xmpproster protocal method will be automatically called
-(Void) xmppRoster :( XMPPRoster *) sender didReceiveBuddyRequest :( XMPPPresence *) presence

How can I refuse to add a friend with xmpp?

Add a package filter, find the Presence package, and then send the Presence. Type. unsubscribed Type Presence package back.

AndFilter presence_sub_filter = new AndFilter (new PacketTypeFilter (Presence. class), new PacketFilter (){
@ Override
Public boolean accept (Packet packet ){
// TODO Auto-generated method stub
Presence p = (Presence) packet; // cast packet to Presence
If (p. getType (). equals (Presence. Type. subscribe )){
Return true;
}
Return false;
}
});
Connection. addPacketListener (new PacketListener (){
@ Override
Public void processPacket (Packet packet ){
// TODO Auto-generated method stub
String p_cycler = packet. getFrom ();
Presence presence_back = new Presence (Presence. Type. unsubscribed );
Presence_back.setTo (P_receiver );
Connection. sendPacket (presence_back );
}
}, Presence_sub_filter );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.