Add friends to the XMPP iOS client (3)
Here we record the process of adding friends. Although there are examples on the Internet, they are not very comprehensive, and one of them is not given.
Initialize the XMPPRoster class. I have been searching for the initialization settings of this class for a long time. Not much. Paste the code below.
Initialize the XMPPRoster object Declaration
@ Interface XMPPModel ()
@ Property (nonatomic, strong) XMPPStream * xmppStream; @ property (nonatomic, strong) XMPPRoster * xmppRoster; // user object @ property (nonatomic, strong) Volume * xmppRosterDataStorage; @ end
Implementation 1: Initialize the _ xmppRoster object first,
// Initialization method-(void) setupStream {_ xmppStream = [[XMPPStream alloc] init]; [_ xmppStream addDelegate: self delegateQueue: dispatch_get_main_queue ()]; _ xmppRosterDataStorage = [[XMPPRosterCoreDataStorage alloc] init]; _ xmppRoster = [[XMPPRoster alloc] initWithRosterStorage: _ xmppRosterDataStorage]; // _ xmppRoster. autoFetchRoster = YES; // _ xmppRoster. autoAcceptKnownPresenceSubscriptionRequests = YES ;//}
Implementation 2: The following is a successful login callback. Add the following code to the callback:
// Login successful-(void) xmppStreamDidAuthenticate :( XMPPStream *) sender {[self goOnline]; NSLog (@ login successful); [[NSUserDefaults standardUserDefaults] setObject: @ success forKey: XMPPLogin]; [_ xmppRoster activate: _ xmppStream]; [_ xmppRoster addDelegate: self delegateQueue: dispatch_get_main_queue ()];}
The above code associates the _ xmppRoster object with _ xmppStream. In this way, you can add friends. Note that
[_ XmppRosteractivate: _ xmppStream]; this line of code should be written in the method of successful login callback. If it is written in Initialization
-(Void) The setupStream method will have an endless loop.
The following shows how to add friends and process friend requests. There are many codes below, mainly the code above.
//// XMPPModel + Friend. m // XMPPTest /// Created by qitmac000246 on 12/26/14. // Copyright (c) 2014 du jia. all rights reserved. // # import XMPPModel + Friend. h @ implementation XMPPModel (Friend) // Add Friend-(void) addFriend :( NSString *) jidString xmppRoster :( XMPPRoster *) xmppRoster {pai* jid = [NSString stringWithFormat: @ % @, jidString, @ shenqi]; [xmppRoster subscribePresenceToUser: jid];} // Accept friend request-(void) receiveContact :( XMPPStream *) sender presence :( XMPPPresence *) presence xmppRoster :( XMPPRoster *) xmppRoster {NSString * presenceType = presence. type; NSString * userId = sender. myJID. user; NSString * presenceFromUser = presence. from. user; if (! [PresenceFromUser isinclutostring: userId]) {// user online if ([presenceType isinclutostring: @ available]) {} else if ([presenceType isinclutostring: @ unavailable]) {} else if ([presenceType isw.tostring: @ subscribe]) {// NSLog (@%@, presence. description); // NSLog (% @, presence. from); XMPPJID * jid = [XMPPJID jidWithString: [NSString stringWithFormat:, presence. from]; [xmppRoster acceptPresenceSubscriptionRequestFrom: jid andAddToRoster: YES] ;}}@ end