XMPP iOS Client Add Friends (3)

Source: Internet
Author: User

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)

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.