您的位置:首页 > 其它

03环信好友管理 - 删除好友

2016-02-13 16:50 288 查看
要实现的效果:



方法:

#pragma mark - tableViewDelegate
/**
*  实现这个方法,滑动表格cell就会出现Delete按钮
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete ) {
// 获取要删除的好友的名字
EMBuddy *buddy = self.buddyList[indexPath.row];
NSString *username = buddy.username;

// 删除好友
//removeFromRemote:是否将自己从对方好友列表中移除
[[EaseMob sharedInstance].chatManager removeBuddy:username removeFromRemote:YES error:nil];
}
}
最后联系人控制器代码如下:
//
//  ContactViewController.m

#import "ContactViewController.h"
#import "EaseMob.h"

@interface ContactViewController ()<EMChatManagerDelegate>
/**
*  好友列表数据源
*/
@property(nonatomic,strong)NSArray *buddyList;
@end

@implementation ContactViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 标题
self.title = @"联系人";

// 添加(聊天管理器)代理
[[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];

// 获取好友列表数据
self.buddyList = [[EaseMob sharedInstance].chatManager buddyList];
}

/**
*  移除(聊天管理器)代理
*/
- (void)dealloc
{
[[EaseMob sharedInstance].chatManager removeDelegate:self];
}

#pragma mark - EMChatManagerDelegate
/*!
@method
@brief 好友请求被接受时的回调
@discussion
@param username 之前发出的好友请求被用户username接受了
*/
- (void)didAcceptedByBuddy:(NSString *)username
{
NSString *message = [NSString stringWithFormat:@"%@ 同意了你的好友请求",username];

// 提示
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"好友添加消息" message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];

// 把新的好友显示到表格
[[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
if (!error) {
// 赋值数据源
self.buddyList = buddyList;

// 刷新表格
[self.tableView reloadData];
}
} onQueue:nil];
}

/*!
@method
@brief 好友请求被拒绝时的回调
@discussion
@param username 之前发出的好友请求被用户username拒绝了
*/
- (void)didRejectedByBuddy:(NSString *)username
{
NSString *message = [NSString stringWithFormat:@"%@ 拒绝了你的好友请求",username];

// 提示
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"好友添加消息" message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}

/*!
@method
@brief 接收到好友请求时的通知
@discussion
@param username 发起好友请求的用户username
@param message  收到好友请求时的say hello消息
*/
- (void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message
{
// 弹出提示,让用户选择“同意”还是“拒绝”
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"好友添加请求" message:message preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"同意" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// 同意加好友申请
[[EaseMob sharedInstance].chatManager acceptBuddyRequest:username error:nil];

}]];
[alert addAction:[UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// 拒绝加好友申请
[[EaseMob sharedInstance].chatManager rejectBuddyRequest:username reason:@"我不认识你" error:nil];

}]];

[self presentViewController:alert animated:YES completion:nil];
}

/**
*  当其他用户向你发出“加好友”请求,你同意后会调用这个方法
*  当你“删除好友”之后也会调用这个方法
*/
- (void)didUpdateBuddyList:(NSArray *)buddyList changedBuddies:(NSArray *)changedBuddies isAdd:(BOOL)isAdd
{

// 重新赋值数据源
self.buddyList = buddyList;

// 刷新表格
[self.tableView reloadData];
}

/**
*  好友删除了你会调用这个方法
*/
- (void)didRemovedByBuddy:(NSString *)username
{
// 当好友删除了你,一般我们不会提示,但会刷新你联系人数据

// 把新的好友显示到表格
[[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
if (!error) {
// 赋值数据源
self.buddyList = buddyList;

// 刷新表格
[self.tableView reloadData];
}
} onQueue:nil];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.buddyList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"buddyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

// 1.获取"好友"模型
EMBuddy *buddy = self.buddyList[indexPath.row];

// 2.配置cell数据
cell.imageView.image = [UIImage imageNamed:@"chatListCellHead"];
cell.textLabel.text = buddy.username;

return cell;
}

#pragma mark - tableViewDelegate
/**
*  实现这个方法,滑动表格cell就会出现Delete按钮
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete ) {
// 获取要删除的好友的名字
EMBuddy *buddy = self.buddyList[indexPath.row];
NSString *username = buddy.username;

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除好友" message:@"确定要删除该好友?" preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// 删除好友
//removeFromRemote:是否将自己从对方好友列表中移除
[[EaseMob sharedInstance].chatManager removeBuddy:username removeFromRemote:YES error:nil];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: