here to record my implementation of the process of adding friends, although there are corresponding examples on the Internet, but not very comprehensive, one of which has not given
Initialization of the Xmpproster class. I looked for the initialization settings for this class for a long time. Not much to say. Let's start with the code.
Initializing the xmpproster object declaration
@interface Xmppmodel () <XMPPStreamDelegate,XMPPRosterDelegate> @property (nonatomic, strong) Xmppstream * Xmppstream; @property (nonatomic, strong) Xmpproster *xmpproster; User Object @property (nonatomic, strong) Xmpprostercoredatastorage *xmpprosterdatastorage; @end
Implementation 1 Here is just the first initialization of the _xmpproster object,
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 Below is the login successful callback, add the following code in the following callback:
Login Success-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender{ [self goonline]; NSLog (@ "landing success"); [[Nsuserdefaults Standarduserdefaults] setobject:@ "Success" forkey:xmpplogin]; [_xmpproster Activate:_xmppstream]; [_xmpproster adddelegate:self Delegatequeue:dispatch_get_main_queue ()]; }
The code above is to associate the _xmpproster object with the _xmppstream. This makes it possible to add a friend,Note
[_xmpprosteractivate:_xmppstream]; This line of code should be written in the method of the login successful callback, if the write in the initialization
-(void) Setupstream There is a dead loop in this method.
Below in the paste add friends and processing Friend request method, the following code Baidu on a lot, the main is the above code.
xmppmodel+friend.m//xmpptest////Created by qitmac000246 on 12/26/14.//Copyright (c) 2014 DuPont. All rights reserved.//#import "Xmppmodel+friend.h" @implementation Xmppmodel (Friend)//Add friend-(void) Addfriend: ( NSString *) jidstring xmpproster: (Xmpproster *) xmpproster{Xmppjid *jid = [Xmppjid jidwithstring:[nsstring stringWithFor mat:@ "%@@%@", 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 Isequaltostring:userid]) {//user online if ([Presencetype isequaltostring:@ "available "]) {}else if ([Presencetype isequaltostring:@" Unavailable "]) {} else if ([Presencetype isequaltostring:@] SubsCribe "]) {//NSLog (@"%@ ", presence.description); NSLog (@ "%@", Presence.from); Xmppjid *jid = [Xmppjid jidwithstring:[nsstring stringwithformat:@ "%@", Presence.from]]; [Xmpproster Acceptpresencesubscriptionrequestfrom:jid Andaddtoroster:yes]; }}} @end
Original address: http://write.blog.csdn.net/postedit/42271089
XMPP iOS Client Add Friends (3)