iOS开发-环信群聊集成

一、加载群列表

1. 新建一个群列表的界面

新建一个类,取名叫做GroupChatViewController,因为这是个列表,所以继承于列表基类YCBaseTableViewController。在GroupChatViewController.m中添加如下代码:

#import "GroupChatViewController.h"

@interface GroupChatViewController ()

@end

@implementation GroupChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我的群";

    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                             target:self
                                                                             action:@selector(btnCreateGroup:)];
    self.navigationItem.rightBarButtonItem = btnItem;

}

#pragma mark - Private Menthods

//新建一个群
- (void)btnCreateGroup:(id)sender
{

}

@end
2. 加载群列表
1、环信官方文档提供加载群有五种方法,这是异步加载的方法,如下:
[[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
    if (!error) {
        NSLog(@"获取成功 -- %@",groups);
    }
} onQueue:nil
2、在我的工程中实现加载群列表

在GroupChatViewController.m文件中添加如下代码:

#import "GroupChatViewController.h"
#import "ChatViewController.h"

@interface GroupChatViewController ()
{
    NSArray *arrGroup;
}
@end

@implementation GroupChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我的群";

    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                             target:self
                                                                             action:@selector(btnCreateGroup:)];
    self.navigationItem.rightBarButtonItem = btnItem;

    //获取所有的群
    [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
        if (!error) {
            arrGroup = [NSArray arrayWithArray:groups];
            [self.tableView reloadData];
        }
    } onQueue:nil];

}

#pragma mark - UITableView Delegate & Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arrGroup.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    EMGroup *group = [arrGroup objectAtIndex:indexPath.row];
    NSString *imageName = @"group_header";

    cell.imageView.image = [UIImage imageNamed:imageName];
    if (group.groupSubject && group.groupSubject.length > 0) {
        cell.textLabel.text = group.groupSubject;
    }
    else {
        cell.textLabel.text = group.groupId;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    EMGroup *group = [arrGroup objectAtIndex:indexPath.row];
    ChatViewController *chatController = [[ChatViewController alloc] initWithChatter:group.groupId isGroup:YES];
    chatController.title = group.groupSubject;
    [self.navigationController pushViewController:chatController animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.0f;
}
#pragma mark - Private Menthods

//新建一个群
- (void)btnCreateGroup:(id)sender
{

}

@end
3、添加跳转

在AddressBookViewController.m中点击列表的方法中,添加一段代码跳转的方法。首先添加头文件:

#import "GroupChatViewController.h"

然后添加跳转的代码:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                SystemNotificationViewController *sysVC = [[SystemNotificationViewController alloc] init];
                [self.navigationController pushViewController:sysVC animated:YES];
                break;
            }
            case 1:
            {
                GroupChatViewController *groupChatVC = [[GroupChatViewController alloc] init];
                [self.navigationController pushViewController:groupChatVC animated:YES];
                break;
            }

            default:
                break;
        }
    }else {
        EMBuddy *buddy = [arrFriends objectAtIndex:indexPath.row];

        ChatViewController *chatVC = [[ChatViewController alloc] initWithChatter:buddy.username isGroup:NO];
        chatVC.title = buddy.username; //好友的名字

        [self.navigationController pushViewController:chatVC animated:YES];
    }
}

如果你的工程运行没有数据,是因为你的账号里面没有群,先去环信后台新建几个群。
这里写图片描述

二、新建群

1. 新建一个创群的界面
1、 新建一个类,取名叫做CreateGoupViewController,是一个列表类,所以基于YCBaseTableViewController。添加创建UI的代码,如下:
#import "CreateGroupViewController.h"

@interface CreateGroupViewController ()
{
    NSArray *arrFriends;
}

@property (retain, nonatomic) UITextField *textFieldName;
@property (retain, nonatomic) UITextView *textViewDes;
@property (retain, nonatomic) UIActivityIndicatorView *activityView;
@end

@implementation CreateGroupViewController
@synthesize textFieldName,textViewDes;

- (void)viewDidLoad {
    [super viewDidLoad];

    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activityView.center = self.tableView.center;
    _activityView.color = [UIColor blackColor];

    [self.view addSubview:_activityView];


    self.title = @"新建群";

    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                             target:self
                                                                             action:@selector(btnClickDone:)];
    self.navigationItem.rightBarButtonItem = btnItem;



}

#pragma mark - UITableViewDelegate & UITableViewDataSource


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 170.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [YCCommonCtrl commonViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 165) backgroundColor:kColor_White];

    textFieldName = [YCCommonCtrl commonTextFieldWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 35)
                                                            placeholder:@"群聊名称"
                                                                  color:kColor_Gray
                                                                   font:kFont_Title
                                                        secureTextEntry:NO
                                                               delegate:self];

    [YCCommonCtrl setViewBorderWithView:textFieldName borderColor:kColor_LightGray borderWidth:1.0f cornerRadius:5.0f];
    [headerView addSubview:textFieldName];

    textViewDes = [YCCommonCtrl commonTextViewWithFrame:CGRectMake(10, 55, SCREEN_WIDTH-20, 80)
                                                               text:@""
                                                              color:kColor_Gray
                                                               font:kFont_Title
                                                      textAlignment:NSTextAlignmentLeft];
    [YCCommonCtrl setViewBorderWithView:textViewDes borderColor:kColor_LightGray borderWidth:1.0f cornerRadius:5.0f];
    [headerView addSubview:textViewDes];

    UILabel *label = [YCCommonCtrl commonLableWithFrame:CGRectMake(10, 150, SCREEN_WIDTH-20, 15)
                                                    text:@"请选择群成员"
                                                   color:kColor_Blue
                                                    font:kFont_Large
                                            textAlignment:NSTextAlignmentLeft];
    [headerView addSubview:label];

    return headerView;
}



#pragma mark - Private Menthods

- (void)btnClickDone:(id)sender
{

}
2、设置跳转

在GroupChatViewController.m中增加一个跳转的方法。
首先GroupChatViewController.m导入头文件:

#import "CreateGroupViewController.h"

然后,在GroupChatViewController.m中的btnCreateGroup方法中,添加代码:

//新建一个群
- (void)btnCreateGroup:(id)sender
{
    CreateGroupViewController *createGroupVC = [[CreateGroupViewController alloc] init];
    [self.navigationController pushViewController:createGroupVC animated:YES];
}
2. 选择群成员

建好群之后,还可以选择一些群成员,当然这些群成员是从你的好友列表中去选择,所以我们这里需要进行加载好友列表。创建一个arrSelected数组用来保存选中的好友。代码如下:

#import "CreateGroupViewController.h"

@interface CreateGroupViewController ()
{
    NSArray *arrFriends;
    NSMutableArray *arrSelected;
}

@property (retain, nonatomic) UITextField *textFieldName;
@property (retain, nonatomic) UITextView *textViewDes;
@property (retain, nonatomic) UIActivityIndicatorView *activityView;
@end

@implementation CreateGroupViewController
@synthesize textFieldName,textViewDes;

- (void)viewDidLoad {
    [super viewDidLoad];

    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activityView.center = self.tableView.center;
    _activityView.color = [UIColor blackColor];

    [self.view addSubview:_activityView];


    self.title = @"新建群";

    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                             target:self
                                                                             action:@selector(btnClickDone:)];
    self.navigationItem.rightBarButtonItem = btnItem;

    arrSelected = [[NSMutableArray alloc] init];

    //获取好友列表
    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        if (!error) {
            arrFriends = [NSArray arrayWithArray:buddyList];
            [self.tableView reloadData];
        }
    } onQueue:nil];



}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arrFriends.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    EMBuddy *eMBuddy = [arrFriends objectAtIndex:indexPath.row];
    cell.textLabel.text = eMBuddy.username;
    cell.imageView.image = [UIImage imageNamed:@"chatListCellHead"];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; //得到当前选中的cell

    if (cell.accessoryType  == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [arrSelected removeObject:[arrFriends objectAtIndex:indexPath.row]];
    }else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [arrSelected addObject:[arrFriends objectAtIndex:indexPath.row]];
    }

    NSLog(@"%ld",arrSelected.count);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 170.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [YCCommonCtrl commonViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 165) backgroundColor:kColor_White];

    textFieldName = [YCCommonCtrl commonTextFieldWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 35)
                                                            placeholder:@"群聊名称"
                                                                  color:kColor_Gray
                                                                   font:kFont_Title
                                                        secureTextEntry:NO
                                                               delegate:self];

    [YCCommonCtrl setViewBorderWithView:textFieldName borderColor:kColor_LightGray borderWidth:1.0f cornerRadius:5.0f];
    [headerView addSubview:textFieldName];

    textViewDes = [YCCommonCtrl commonTextViewWithFrame:CGRectMake(10, 55, SCREEN_WIDTH-20, 80)
                                                               text:@""
                                                              color:kColor_Gray
                                                               font:kFont_Title
                                                      textAlignment:NSTextAlignmentLeft];
    [YCCommonCtrl setViewBorderWithView:textViewDes borderColor:kColor_LightGray borderWidth:1.0f cornerRadius:5.0f];
    [headerView addSubview:textViewDes];

    UILabel *label = [YCCommonCtrl commonLableWithFrame:CGRectMake(10, 150, SCREEN_WIDTH-20, 15)
                                                    text:@"请选择群成员"
                                                   color:kColor_Blue
                                                    font:kFont_Large
                                            textAlignment:NSTextAlignmentLeft];
    [headerView addSubview:label];

    return headerView;
}

#pragma mark - Private Menthods

- (void)btnClickDone:(id)sender
{

}


@end
3. 创群

终于到了最后一步了,在btnClickDone方法中添加创群的接口代码:

#pragma mark - Private Menthods

- (void)btnClickDone:(id)sender
{
    NSMutableArray *arrName = [[NSMutableArray alloc] init];
    for (EMBuddy *eMBuddy in arrSelected ) {
        [arrName addObject:eMBuddy.username];
    }

    EMGroupStyleSetting *groupStyleSetting = [[EMGroupStyleSetting alloc] init];
    groupStyleSetting.groupMaxUsersCount = 500;
    groupStyleSetting.groupStyle = eGroupStyle_PrivateOnlyOwnerInvite;

    [self.activityView startAnimating];

    [[EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:textFieldName.text
                                                          description:textViewDes.text
                                                             invitees:arrName
                                                initialWelcomeMessage:@"hello"
                                                         styleSetting:groupStyleSetting
                                                           completion:^(EMGroup *group, EMError *error) {
                                                               if(!error){

                                                                   [self.activityView stopAnimating];

                                                                   UIAlertController *alterController = [UIAlertController alertControllerWithTitle:nil
                                                                                                                                            message:@"创建成功"
                                                                                                                                     preferredStyle:UIAlertControllerStyleAlert];
                                                                   [self presentViewController:alterController animated:YES completion:nil];

                                                                   UIAlertAction *alterAction = [UIAlertAction actionWithTitle:@"确定"
                                                                                                                         style:UIAlertActionStyleDefault
                                                                                                                       handler:^(UIAlertAction * _Nonnull action) {
                                                                                                                           [self.navigationController popViewControllerAnimated:YES];
                                                                                                                       }];
                                                                   [alterController addAction:alterAction];


                                                               }
                                                           }
                                                              onQueue:nil];
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中,支付是一个非常重要的功能。在开发支付时,需要考虑到支付流程、支付方式、支付安全等方面的内容。以下是一些开发支付的要点: 1. 集成支付SDK iOS开发中,一般使用第三方支付SDK来实现支付功能。常见的支付SDK包括:支付宝SDK、微信支付SDK、银联支付SDK等。在使用SDK前,需要先注册开发者账号,并获取相应的API Key和App ID等信息。 2. 支付流程 支付流程一般包括以下几个步骤: - 用户选择支付方式; - 向支付平台发起支付请求; - 用户输入支付密码; - 支付平台返回支付结果; - 应用根据支付结果进行相应的处理。 3. 支付安全 支付安全是非常重要的。在开发中,需要考虑到以下方面: - 用户信息的安全保护:包括用户的账号、密码、支付信息等; - 支付数据的安全保护:对于涉及到支付的数据,需要采用加密算法进行保护,避免被非法攻击者窃取; - 安全审计:需要对支付过程中的各个环节进行安全审计,及时发现并修复漏洞。 4. 支付方式 在iOS开发中,常见的支付方式包括: - 支付宝支付:支持PC端、移动端、扫码支付等多种支付方式; - 微信支付:支持微信内支付、H5支付、APP支付等多种支付方式; - 苹果支付:支持应用内购买,用户可以直接使用Apple ID进行支付。 需要根据应用的实际情况,选择适合的支付方式。 总之,开发支付需要考虑到多个方面的内容,包括支付流程、支付方式、支付安全等,需要仔细规划和实现,以保证支付功能的正常运作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值