获取好友列表,环信提供了4种方法。
从本地获取:该方法比较特殊,只有在您之前获取过好友列表的情况下才会有值,且不能保证最新。
NSArray *buddyList = [[EaseMob sharedInstance].chatManager buddyList];
另外3种都是从服务器上获取:
//1. 同步方法
EMError *error = nil;
NSArray *buddyList = [[EaseMob sharedInstance].chatManager fetchBuddyListWithError:&error];
if (!error) {
NSLog(@"获取成功 -- %@",buddyList);
}
//2. block异步方法
[[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
if (!error) {
NSLog(@"获取成功 -- %@",buddyList);
}
} onQueue:nil];
//3.IChatManagerDelegate回调方法
[[EaseMob sharedInstance].chatManager asyncFetchBuddyList];
一.注意点
1.如果当前有添加好友请求,环信SDK内部会往本地数据库buddy表插入好友记录。
2.如果程序删除获取用户第一次登陆,那么buddy表是没有记录的,所以要从服务器获取好友列表数据
3.所以buddyList没有值的情况:用户第一次登陆;自动登陆还没有完成。
二.解决方案
1.从服务器获取好友列表数据
2.用户第一次登陆后,自动从服务器获取好友列表:
// 登录成功后,自动去取好友列表
[[EaseMob sharedInstance].chatManager setIsAutoFetchBuddyList:YES];
所以我们修改LoginViewController的登录方法:
/**
* 登录
*/
- (IBAction)login:(UIButton *)sender
{
NSString *username = self.usernameField.text;
NSString *password = self.passwordField.text;
if (username.length == 0 || password.length == 0) {
NSLog(@"请输入账号和密码");
return;
}
// 1.让环信的SDK在第一次登陆完成之后,自动从服务获取好友列表同步到本地数据库buddy表
[[EaseMob sharedInstance].chatManager setIsAutoFetchBuddyList:YES];
// 2.登录(block异步方法)
[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:username password:password completion:^(NSDictionary *loginInfo, EMError *error) {
if (!error && loginInfo) {
// 设置自动登录
[[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
// 跳转到主界面
self.view.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];
}
} onQueue:nil];
}
3.联系人控制器tableView展示数据:
//
// 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];
}
/*!
@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];
}
#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;
}
@end