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

2017-04-22  本文已影响124人  陈水寒

获取好友列表

获取好友列表有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
上一篇下一篇

猜你喜欢

热点阅读