XMPPRoster類的初始化。我找這個類的初始化設置找了好久。不多說啦。下面開始貼代碼啦。
初始化XMPPRoster 對象聲明
@interface XMPPModel()@property (nonatomic , strong) XMPPStream *xmppStream; @property (nonatomic , strong) XMPPRoster *xmppRoster; //用戶對象 @property (nonatomic , strong) XMPPRosterCoreDataStorage *xmppRosterDataStorage; @end
// 初始化方法 - (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; // }
// 登陸成功 - (void) xmppStreamDidAuthenticate:(XMPPStream *)sender { [self goOnline]; NSLog(@登陸成功); [[NSUserDefaults standardUserDefaults] setObject:@success forKey:XMPPLogin]; [_xmppRoster activate:_xmppStream]; [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()]; }
[_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