添加好友
加好友其实就是和别人建立关系,也就是xmpp的订阅,那么我们就要找个方法和别人发生点什么~ ~ ~ 那就是下面这个方法了:
XMPPJID *jid = [XMPPJID jidWithUser:self.friendSearchTextField.text domain:KDomin resource:kResource];
[[XMPPManager sharedInstance].roster subscribePresenceToUser:jid];
这里的roster是XMPPRoster的一个实例,要在初始化的时候激活
//实例化XMPPRoster
self.roseterDataStorage = [XMPPRosterCoreDataStorage sharedInstance];
self.roster = [[XMPPRoster alloc]initWithRosterStorage:self.roseterDataStorage dispatchQueue:dispatch_get_main_queue()];
//激活
[self.roster activate:self.stream];
[self.roster addDelegate:self delegateQueue:dispatch_get_main_queue()];
同意/拒绝好友请求
好了,约会请求已经发送了,那么小姐姐是如何收到你的约会邀请的呢?
在xmppRosterDelegate代理方法里有这么一个方法
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence
{
//好友申请,from:发送好友的jid
self.subscrptionRequestJID = presence.from;
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"添加好友申请" message:presence.from.user delegate:self cancelButtonTitle:@"拒绝" otherButtonTitles:@"同意并添加对方", nil];
[alertview show];
//在alertview的代理方法中处理添加请求
}
#pragma mark --UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
//拒绝
[self.roster rejectPresenceSubscriptionRequestFrom:self.subscrptionRequestJID];
break;
case 1:
//同意
[self.roster acceptPresenceSubscriptionRequestFrom:self.subscrptionRequestJID andAddToRoster:YES];
default:
break;
}
self.subscrptionRequestJID = nil;
}
收到对方处理回执
到这里有人肯定要问了:说小姐姐到底跟我约不约啊,我咋知道呢?
在XMPPStreamDelegate里有这么个方法,当别人在线状态改变,或者接受,婉拒了你的请求,它会收到通知
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
//上线状态 - "available"
//离线状态 - "unavailable"
//同意 - "subscribed"
//拒绝 - "unsubscribed"
if ([presence.type isEqualToString:@"subscribed"]) {
[self showAlertWithTitle:[NSString stringWithFormat:@"%@同意了您的请求",presence.from.user]];
//刷新列表
}else if ([presence.type isEqualToString:@"unsubscribed"]) {
[self showAlertWithTitle:[NSString stringWithFormat:@"%@拒绝了您的请求",presence.from.user]];
}
}
好了,到这里差不多就大结局了! 啥?你说啥?
"大结局后通常应该有点彩蛋"
"哦,那我准备一下"
//其实吧添加好友还有这么一个方法
[[XMPPManager sharedInstance].roster addUser:jid withNickname:jid.user]
网友评论