iOS开发-环信单聊实现

一、搭建基本框架

1、新建三个UIViewController

新建三个ViewController,继承UIViewController,分别命名为:FirstViewController,SecondViewController,ThirdViewController。如下图所示:
这里写图片描述

2、添加登陆方法

在AppDelegate.m中添加如下代码:

#define APPKEY      @"1101#testrongyun"     //环信APPKEY
#define APNSCert    @"TestHuanXin"          //环信推送证书名称

#import "AppDelegate.h"
#import "EaseMob.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //初始化环信SDK
    [[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:APNSCert];

    //异步登陆的方法
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:@"yuancan001" password:@"123" completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
            NSLog(@"登陆成功");
            [self setUpNav];
        }
    } onQueue:nil];
    return YES;
}

- (void)setUpNav
{
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];

    firstVC.title = @"会话列表";
    secondVC.title = @"通讯录";
    thirdVC.title = @"设置";

    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.viewControllers = @[[[UINavigationController alloc] initWithRootViewController:firstVC],
                               [[UINavigationController alloc] initWithRootViewController:secondVC],
                               [[UINavigationController alloc] initWithRootViewController:thirdVC]];
    self.window.rootViewController = tabBar;
    self.window.backgroundColor = [UIColor whiteColor];

}
@end

编译一下,看下效果。
这里写图片描述

二、添加与聊天有关的文件

1、添加GifImage文件

这里写图片描述

2、添加chat文件

这里写图片描述
添加完成之后,编译一下,把报错的地方全部注释掉,有很多地方需要注释掉,这些地方是因为有些我们不需要的文件没有添加进来。
点击下载注释好的GifImage和chat文件

三、实现单聊

在SecondViewController.m中添加如下代码:

#import "SecondViewController.h"
#import "ChatViewController.h"

@interface SecondViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *arrSystem;
    NSArray *arrFriends;
}

@property (retain, nonatomic)  UITableView *tableView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    arrSystem = @[@"申请与通知",@"群聊",@"聊天室"];

    _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    //获取好友列表

    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        if (!error) {
            NSLog(@"获取成功 -- %@",buddyList);
            arrFriends = [NSArray arrayWithArray:buddyList];
            [_tableView reloadData];
        }
    } onQueue:nil];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        return arrSystem.count;
    } else {
        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];
    }

    switch (indexPath.section) {
        case 0:
        {
            cell.textLabel.text = [arrSystem objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"groupPublicHeader"];
            break;
        }

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

        default:
            break;
    }

    return cell;
}

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

    EMBuddy *buddy = [arrFriends objectAtIndex:indexPath.row];

    ChatViewController *chatVC = [[ChatViewController alloc] initWithConversationChatter:buddy.username conversationType:eConversationTypeChat];
    chatVC.title = buddy.username; //好友的名字
    chatVC.hidesBottomBarWhenPushed = YES;

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

OK,单聊已经集成成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值