XMPPStream *xmppStream;
XMPPReconnect *xmppReconnect;
XMPPRoster *xmppRoster;//用户对象
//需要添加的对象
//添加好友
#pragma mark 加好友
- (void)XMPPAddFriendSubscribe:(NSString *)name
{
//XMPPHOST 就是服务器名, 主机名
XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",name,XMPPHOST]];
//[presence addAttributeWithName:@"subscription" stringValue:@"好友"];
[xmppRoster subscribePresenceToUser:jid];
}
//处理加好友
#pragma mark 处理加好友回调,加好友
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence
{
//取得好友状态
NSString *presenceType = [NSString stringWithFormat:@"%@", [presence type]]; //online/offline
//请求的用户
NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]];
NSLog(@"presenceType:%@",presenceType);
NSLog(@"presence2:%@ sender2:%@",presence,sender);
XMPPJID *jid = [XMPPJID jidWithString:presenceFromUser];
[xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES];
}
//在次处理加好友
#pragma mark 收到好友上下线状态
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
// DDLogVerbose(@"%@: %@ ^^^ %@", THIS_FILE, THIS_METHOD, [presence fromStr]);
//取得好友状态
NSString *presenceType = [NSString stringWithFormat:@"%@", [presence type]]; //online/offline
//当前用户
// NSString *userId = [NSString stringWithFormat:@"%@", [[sender myJID] user]];
//在线用户
NSString *presenceFromUser =[NSString stringWithFormat:@"%@", [[presence from] user]];
NSLog(@"presenceType:%@",presenceType);
NSLog(@"用户:%@",presenceFromUser);
//这里再次加好友
if ([presenceType isEqualToString:@"subscribed"]) {
XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@",[presence from]]];
[xmppRoster acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES];
}
}
#pragma mark 删除好友,取消加好友,或者加好友后需要删除
- (void)removeBuddy:(NSString *)name
{
XMPPJID *jid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@",name,XMPPHOST]];
[self xmppRoster] removeUser:jid];
}
注意主机名 和 处理顺序