Add friends to the XMPP iOS client (3)

Source: Internet
Author: User

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

 

Related Article

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.