XMPP iOS用戶端 添加好友 (3),xmppios

來源:互聯網
上載者:User

XMPP iOS用戶端 添加好友 (3),xmppios
這裡記錄下我實現添加好友的過程,網上雖然也有相應的例子,但說的不是很全面,其中有一篇沒有給出

XMPPRoster類的初始化。我找這個類的初始化設定找了好久。不多說啦。下面開始貼代碼啦。


初始化XMPPRoster   對象聲明

@interface XMPPModel()<XMPPStreamDelegate,XMPPRosterDelegate>@property (nonatomic , strong) XMPPStream *xmppStream;@property (nonatomic , strong) XMPPRoster *xmppRoster; //使用者物件@property (nonatomic , strong) XMPPRosterCoreDataStorage *xmppRosterDataStorage;@end

實現1  這裡只是先初始化_xmppRoster 這個對象,

// 初始化方法- (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;//}

實現2 下面是登陸成功的回調,在下面的回調中添加下面代碼:

// 登陸成功- (void) xmppStreamDidAuthenticate:(XMPPStream *)sender{    [self goOnline];    NSLog(@"登陸成功");    [[NSUserDefaults standardUserDefaults] setObject:@"success" forKey:XMPPLogin];    [_xmppRoster activate:_xmppStream];    [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];           }

上面的代碼作用是將_xmppRoster對象與_xmppStream聯絡在一起。這樣就能實現添加好友了,注意 

[_xmppRosteractivate:_xmppStream]; 這行代碼要寫在登陸成功回調的方法裡,如果寫在初始化

- (void)setupStream 這個方法裡會出現死迴圈。

下面在貼下添加好友和處理好友請求的方法,下面的代碼百度上又很多,主要的是上面的代碼。

////  XMPPModel+Friend.m//  XMPPTest////  Created by qitmac000246 on 12/26/14.//  Copyright (c) 2014 杜甲. All rights reserved.//#import "XMPPModel+Friend.h"@implementation XMPPModel (Friend)// 添加好友- (void)addFriend:(NSString *)jidString xmppRoster:(XMPPRoster *)xmppRoster{    XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",jidString,@"shenqi"]];            [xmppRoster subscribePresenceToUser:jid];}//接受好友請求- (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])    {                // 使用者線上        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

原文地址:http://write.blog.csdn.net/postedit/42271089

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.