美文网首页
环信3.0--添加好友及通过

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

作者: 陈水寒 | 来源:发表于2017-04-22 14:14 被阅读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

    相关文章

      网友评论

          本文标题:环信3.0--添加好友及通过

          本文链接:https://www.haomeiwen.com/subject/iorszttx.html