上一节我们完成了读取和显示好友列表功能,接下来我们实现添加好友功能。
adium向手机发起添加好友请求时,会导致手机程序hang住,无法正确添加好友,和managedObjectContext有关,换用spark后,就可以成功的从电脑客户端向手机客户端发起添加好友请求了。
管理好友列表
最常见的是以下两种场景:
- 我们想添加别的用户为好友。
- 别人想添加我们为好友。
对第一种情况,我们需要实现sendInvitationToJID:withNickName:方法,这个方法首先调用XMPPRoster的addUser:withNickName方法来添加JID到好友列表。然后调用XMPPRoster的subscribePrensenceToUser方法来向目标用户发送subscription请求,以请求接收对方的上下线通知。实现如下:
-(void)sendInvitationToJID:(NSString *)_jid withNickName:(NSString *)_nickName
{
[self.xmppRoster addUser:[XMPPJID jidWithString:_jid] withNickname:_nickName];
[self.xmppRoster subscribePresenceToUser:[XMPPJID jidWithString:_jid]];
}
对第二种情况,我们需要实现XMPPRoster的acceptPresenceSubscriptionRequestFrom:andAddToRoster:方法
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence
{
XMPPUserCoreDataStorageObject *user = [self.xmppRosterStorage userForJID:[presence from]
xmppStream:self.xmppStream
managedObjectContext:[self managedObjectContext_roster]];
DDLogVerbose(@"didReceivePresenceSubscriptionRequest from user %@ ", user.jidStr);
[self.xmppRoster acceptPresenceSubscriptionRequestFrom:[presence from] andAddToRoster:YES];
}
邀请好友
接下来我们实现发送邀请的逻辑,在YDContactsViewController中添加一个UIButton,这个按钮对应的事件会弹出一个UIAlertView,用于请求要添加的用户的名称。
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
self.mtableView = [[UITableView alloc] initWithFrame:CGRectMake(0,86,ScreenWidth,ScreenHeight - 86-40) style:UITableViewStylePlain];
self.mtableView .delegate=self;
self.mtableView .dataSource=self;
self.mtableView .rowHeight=38.0;
self.mtableView .separatorStyle=UITableViewCellSeparatorStyleNone;
self.mtableView .backgroundColor = [UIColor clearColor];
[self.view addSubview:self.mtableView];
//Add the invite button
UIButton* inviteButton = [[UIButton alloc] initWithFrame:CGRectMake(20,ScreenHeight - 86-40,280,25)];
inviteButton.backgroundColor=[UIColor blueColor];
inviteButton.layer.borderWidth = 1.0f;
inviteButton.layer.borderColor = [[UIColor grayColor] CGColor];
inviteButton.layer.cornerRadius = 5.0f;
[inviteButton addTarget:self action:@selector(inviteUser:) forControlEvents:UIControlEventTouchUpInside];
UILabel *inviteLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,280,25)];
inviteLabel.backgroundColor=[UIColor clearColor];
[inviteLabel setFont:[UIFont systemFontOfSize:16]];
inviteLabel.text=@"Invite";
inviteLabel.adjustsFontSizeToFitWidth=YES;
inviteLabel.textAlignment=NSTextAlignmentCenter;
inviteLabel.textColor=[UIColor whiteColor];
[inviteButton addSubview:inviteLabel];
[self.view addSubview:inviteButton];
}
添加好友按钮处理事件:
-(IBAction)inviteUser:(id)sender
{
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Enter the user name" message:@"e.g peter" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput ;
[alert show];
}
alertView代理
#pragma mark delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput)
{
UITextField* username = [alertView textFieldAtIndex:0];
NSString *jidString = [NSString stringWithFormat:@"%@@%@",username.text,kXMPPServer];
NSLog(@"JID %@",jidString);
[[self appDelegate] sendInvitationToJID:jidString withNickName:username.text];
}
}
编译并测试:
首先添加在Openfire服务器上添加新的用户Lilac,然后使用adium登陆并在模拟器上打开YDContactsViewContoller页面并点击invite按钮:
输入Lilac并点击ok后,adium会收到好友添加邀请:
验证并接受邀请,可以看到模拟器上,Lilac已经成为了在线的好友,而adium也显示rose为好友:
在Openfire服务器上也可以看到,Lilac和Rose相互成为了好友:
实现至此,类图如下: