环信3.0--添加好友及通过

获取好友列表

获取好友列表有2种方式,一种是从服务器获取,一种是从本地数据库获取

    // 请求服务器获取好友列表
    [[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
        if (!aError) {
//            NSLog(@"获取好友成功%);
            self.buddyList = aList;
            
            [self.tableView reloadData];
        }else{
            NSLog(@"获取好友失败%@",aError.errorDescription);
            // 本地数据库获取好友列表
            self.buddyList = [[EMClient sharedClient].contactManager getContacts];
            
            [self.tableView reloadData];
        }
    }];

添加好友处理

需要注册代理才可以监听好友请求

//注册好友回调
[[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
//移除好友回调
[[EMClient sharedClient].contactManager removeDelegate:self];
  • 好友申请
    // 2.向服务器请求加好友
     /**
     *  contact 申请的好友名称
     *  message 申请好友时的留言
     */
    [[EMClient sharedClient].contactManager addContact:contact message:message completion:^(NSString *aUsername, EMError *aError) {
        if (!aError) {
            [SVProgressHUD showSuccessWithStatus:@"好友申请成功"];
        }else{
            NSLog(@"好友申请失败,%@",aError.errorDescription);
        }
    }];
  • 监听好友申请
- (void)friendRequestDidReceiveFromUser:(NSString *)aUsername message:(NSString *)aMessage
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"添加好友请求" message:aMessage delegate:self cancelButtonTitle:@"不同意" otherButtonTitles:@"好的", nil];
    
    [alertView show];
    
    self.contactUsername = aUsername;
}
  • 同意、拒绝好友申请
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) { // 不同意好友请求
        [[EMClient sharedClient].contactManager declineFriendRequestFromUser:self.contactUsername completion:^(NSString *aUsername, EMError *aError) {
            if (!aError) {
                NSLog(@"发送不同意成功");
            }
        }];
    }else{
        [[EMClient sharedClient].contactManager approveFriendRequestFromUser:self.contactUsername completion:^(NSString *aUsername, EMError *aError) {
            if (!aError) {
                NSLog(@"发送同意成功");
            }
        }];
    }
}

演示效果如下:

添加好友演示.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,668评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,773评论 25 709
  • 六.集成SDK基础功能 (仔细研习,结合实际操作) 由于环信官方文档比较详细,因此,根据项目需求,把用到的方法记录...
    夜空已沉寂阅读 6,478评论 0 7
  • (一) “哇哇哇,哇哇哇……”深夜一声响亮的啼哭声响彻整个马鞍村。 “听这声音难不成他王婶儿家又添了一个女娃娃?...
    知了小姐阅读 3,529评论 5 2
  • 杜鹃花发映山红。 韶光觉正浓。 水流红紫各西东。 绿肥春已空。 闲戏蝶,懒游蜂。破除花影重。 问春何事不从容。忧愁...
    罗俊美阅读 3,627评论 0 3