基于环信实现在线聊天功能

681 阅读13分钟

由于最近接触到的项目需要用到聊天功能,关于聊天的SDK跟安卓同事统一,最终选择了环信。在官方下载好SDK,并研究了一波,最终实现自定义聊天功能。 ###实现前准备 1.下载环信SDK,我使用的版本是V3.2.0 2016-10-15,并且一开始我只使用了简版的,没有实时语音视频通讯,没有支付宝红包。基于环信实现实时语音视频通话功能

2.在下载的文件夹中找到HyphenateSDKEaseUI并在demo中找到emotion表情包,一起拖到新工程中。

3.向Build Phases → Link Binary With Libraries 中添加依赖库。SDK 依赖库有:

CoreMedia.framework
AudioToolbox.framework
AVFoundation.framework
MobileCoreServices.framework
ImageIO.framework
libc++.dylib
libz.dylib
libstdc++.6.0.9.dylib
libsqlite3.dylib

SDK 不支持 bitcode,向 Build Settings → Linking → Enable Bitcode 中设置 NO。在Build Settings-->Other Linker Flags-->双击 填写上:-all_load

4.在工程中设置EaseUI-Prefix.pch的路径。Build Settings-->Prefix Header 复制粘贴路径。如果你工程中有.pch文件,那么就只需要将EaseUI-Prefix.pch中的代码复制进原有的pct文件中就行。(我就只复制了这些)

#define DEMO_CALL 1
#import <UIKit/UIKit.h>
#import <Availability.h>
#import <Foundation/Foundation.h>
#endif

5.在AppDelegate.m中配置环信SDK相关代码。首先在环信后台注册项目,并提交测试版的远程推送证书anps,需要提供的是.p12文件。(推送证书及p12相关,请自行百度)导入头文件#import "EMSDK.h"AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}方法中初始化环信相关配置。

//自动同意加好友.
    BOOL accept = [[EMClient sharedClient].options isAutoAcceptFriendInvitation];
    accept = YES;
    
    //iOS8 注册APNS
    if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
        [application registerForRemoteNotifications];
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
        UIUserNotificationTypeSound |
        UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else{
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
        UIRemoteNotificationTypeSound |
        UIRemoteNotificationTypeAlert;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
    }
    //AppKey:注册的AppKey,详细见下面注释。
    //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。
    
    NSString *apnsCertName = nil;
#if DEBUG
    apnsCertName = @"xxxx";//填写自己的推送证书名字
#else
    apnsCertName = @"XXXX";//同上
#endif
    
    
    EMOptions *options = [EMOptions optionsWithAppkey:@"AppKey"];//填写自己注册的AppKey
    

    options.apnsCertName = apnsCertName;//
    [[EMClient sharedClient] initializeSDKWithOptions:options];

并加上如下代码

#pragma mark - 获取及上传deviceToken
/*!
 @method  系统方法
 @abstract 系统方法,获取deviceToken,并上传环信
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    
    [[EMClient sharedClient] bindDeviceToken:deviceToken];
}
/*!
 @method  系统方法
 @abstract 系统方法,获取deviceToken 失败
 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"error -- %@",error);
}

#pragma mark - 进入前后台 (环信)

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    [[EMClient sharedClient] applicationDidEnterBackground:application];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    [[EMClient sharedClient] applicationWillEnterForeground:application];
    
}

具体的SDK导入过程也就这些,编译一下,如果没报错,就导入成功了。有报错也没关系,找到报错的地方,有时候就是头文件找不到,或者文件重复。仔细看看,把不需要的删掉,出现了连接错误就复制百度。放缓心态,要相信SDK是一定能导入成功的。

这里也把环信的官方文档一并贴上

导入这三个文件夹
###分析需求(一) 我没有用环信demo的UI,大部分是自定义的。因为我觉得自定义的好一些,别人做的再好,那也是别人根据自己的需求做的,不一定满足我们的需求。项目工程不便上传,所以我就根据功能点,一步步地往下写。(根据功能点来,更方便自定义)

第一个面图
从上图中可以看到需要做到的一些功能点有:头像、名称、会话列表、消息、消息发送的时间、未读消息标记;还有一点就是会话列表能左划删除。这六点中名称跟头像环信是不提供的,注册过环信的都知道,它的ID是数字跟字母的组合,没有像这样显示自定义名称的。关于头像,环信服务器也是不提供存储的。 ###实现相关功能(一) 1.登录或注册环信账号,获取会话列表。(账号密码是按照我的项目来的,通过本地数据库存储好友信息,对照着替换)首先导入头文件#import "EMSDK.h"#import "EaseUI.h"

/*!
 @method  登录并加载会话列表。
 @abstract 登录并加载会话列表。
 
 */
- (void)loginAndLoadData
{
    //-----------------------登录逻辑----------------------//
    FDAccountItem *item = [FDLoginTool fd_account];
    NSString *psw       = [item.key substringFromIndex:12];
    
    NSLog(@"psw=%@==%@",psw,item.perName);
    //---首先判断能不能以该身份证为账号进行登录----//
    EMError *error = [[EMClient sharedClient] loginWithUsername:[item.key md5String]  password:[psw md5String]];
    if (!error) {
        //设置远程推送用户名字
        [[EMClient sharedClient] setApnsNickname:item.perName];
        
        if (!error) {
            NSLog(@"添加成功");
        }
        
        NSLog(@"登录成功");
        //自动登录
        [[EMClient sharedClient].options setIsAutoLogin:YES];
        [MBProgressHUD hideHUD];
        
    }
    else
    {
        NSLog(@"%@", error);
        //-------不能以该身份证号为账号登录,就已该身份证号进行注册-------//
        EMError *regError = [[EMClient sharedClient]registerWithUsername:[item.key md5String] password:[psw md5String]];
        
        if (regError ==nil) {
            
            //-----注册成功之后就进行登录-----//
            EMError *logError = [[EMClient sharedClient] loginWithUsername:[item.key md5String] password:[psw md5String]];
            if (!logError) {
                //自动登录
                [[EMClient sharedClient].options setIsAutoLogin:YES];
                [MBProgressHUD hideHUD];
            }
            else
            {
                NSLog(@"登录失败");
                [MBProgressHUD hideHUD];
            }
        }
        else
        {
            NSLog(@"error=%@",error);
            [MBProgressHUD hideHUD];
        }
    }
    //-------------------------end---------------------------//
    //获取所有会话
    NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
    self.dataArr = [[NSMutableArray alloc]initWithArray:conversations];
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];//聊天模块注册监听
    
}

/*!
 @method  环信方法,监听收到消息
 @abstract 不管app前台、后台,收到消息都会来到这个方法
 */
- (void)messagesDidReceive:(NSArray *)aMessages
{
    [self getUnReadCountFromEM];
    [self.tableView reloadData];
    
}
/*!
 @method  从环信获取所有的未读消息
 @abstract 从环信获取所有的未读消息
 */
- (NSInteger)getUnReadCountFromEM {
    NSInteger unReadCount = 0;
    // 获取所有回话列表
    NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
    self.dataArr = [NSMutableArray arrayWithArray:conversations];
    for (EMConversation *conversation in conversations) {
        unReadCount += conversation.unreadMessagesCount;
    }
    if (unReadCount > 0) {
        [self.tabBarController.tabBar huanX_showBadgeOnItemIndex:0];//自定义界面上显示未读消息小红点
    }
    else
    {
        [self.tabBarController.tabBar fd_hideBadgeOnItemIndex:0];//同上
    }
    return unReadCount;
}

2.单元格cell界面的配置以及消息的处理。

XiaoXiTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"XiaoXiTableViewCell" owner:nil options:nil]lastObject];
    
    
    EMConversation *conversation = self.dataArr[indexPath.row];
    
    int a = conversation.unreadMessagesCount;
    if (a > 0) {
        cell.hongDianImage.hidden = NO;
    }else
    {
        cell.hongDianImage.hidden = YES;
    }
    //获得最后一条消息
    EMMessage *message = conversation.latestMessage;
    //获得消息体
    EMMessageBody *messageBody = message.body;
    NSString *str;
    if (messageBody.type==EMMessageBodyTypeText) {
        //消息为文本时,强转为EMTextMessageBody 获取文本消息
        EMTextMessageBody *textBody =(EMTextMessageBody *)messageBody;
        str = [EaseConvertToCommonEmoticonsHelper convertToSystemEmoticons:textBody.text];
        //str = textBody.text;
    }
    //判断消息类型,输出响应类型
    else if (messageBody.type==EMMessageBodyTypeImage)
    {
        str = @"[图片]";
    }
    else if (messageBody.type==EMMessageBodyTypeVoice)
    {
        str = @"[语音]";
    }
    else if (messageBody.type==EMMessageBodyTypeLocation)
    {
        str = @"[位置]";
    }
    else if (messageBody.type==EMMessageBodyTypeVideo)
    {
        str = @"[视频]";
    }
    else if (messageBody.type==EMMessageBodyTypeFile)
    {
        str = @"[文件]";
    }
    else
    {
        str = @"";
    }
    cell.xiaoXiLabel.text = str;
    //-----------------end--------------------//
    //找到会话

    EaseConversationModel *model = [[EaseConversationModel alloc]initWithConversation:conversation];
    //SaveHaoYouModel *haoYouModel = [HaoYouDao selectedFromPeopleTableWithMDsfz:model.title];
    //设置消息时间
    cell.timeLabel.text = [self timeStr:message.timestamp];
    //设置消息发送者昵称
    
    // 打开数据库
    [HaoYouDao createYiShengTable];
    // 搜寻名医模型
    SaveMingYiModel *mYModel = [HaoYouDao selectedFromPeopleTableWithMDsfz:model.title];
    
    NSLog(@"ss%@----%@-",mYModel.docName,mYModel.idcardno);
    
    cell.nameLabel.text = mYModel.docName;
    NSString *imageUrl  = [NSString stringWithFormat:@"https://219.140.163.100:2265/FDAPP/app_upload/%@.jpg",mYModel.idcardno];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:imageUrl]
                      placeholderImage:[UIImage imageNamed:@"touxiang"]];

    //通过扩展消息获得,该消息的扩展消息,即用户的头像,昵称
    //统一设置单元格分割线,和单元格选中样式
    cell.layoutMargins  = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

3.时间的处理。

/*!
 @method  计算消息时间。
 @abstract 计算接收到消息的时间,并判断。
 */
- (NSString *)timeStr:(long long)timestamp
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *currentDate  = [NSDate date];
    
    // 获取当前时间的年、月、日
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger currentYear  = components.year;
    NSInteger currentMonth = components.month;
    NSInteger currentDay   = components.day;
    
    
    NSDate *msgDate = nil;
    // 获取消息发送时间的年、月、日
    if (timestamp == 0) {
        msgDate = currentDate;
    } else {
        msgDate  = [NSDate dateWithTimeIntervalSince1970:timestamp/1000.0];
    }
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
    CGFloat msgYear  = components.year;
    CGFloat msgMonth = components.month;
    CGFloat msgDay   = components.day;
    
    // 判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {
        //今天
        dateFmt.dateFormat = @"HH:mm";
    }else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay ){
        //昨天
        dateFmt.dateFormat = @"昨天 HH:mm";
    }else{
        //昨天以前
        dateFmt.dateFormat = @"yyyy-MM-dd";
    }
    
    return [dateFmt stringFromDate:msgDate];
}

4.删除会话。

/*!
 @method  开启tableview删除功能。
 @abstract 开启tableview删除功能.
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除会话
    EMConversation *conversation = self.dataArr[indexPath.row];
    [[EMClient sharedClient].chatManager deleteConversation:conversation.conversationId isDeleteMessages:YES completion:^(NSString *aConversationId, EMError *aError) {
        
        
    }];
    // 刷新表
    [self.dataArr removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
    
}

5.点击消息,与相应的id进行聊天。

//点击消息,与相应的id进行聊天
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //找到会话
    EMConversation *conversation = self.dataArr[indexPath.row];
    
    EaseConversationModel *model = [[EaseConversationModel alloc]initWithConversation:conversation];
    //从本地数据库中匹配,找到好友信息
    SaveMingYiModel *mingYiModel = [HaoYouDao selectedFromPeopleTableWithMDsfz:model.title];
    //model.title = haoYouModel.name;
    
    EaseMessageViewController *viewController = [[EaseMessageViewController alloc] initWithConversationChatter:model.conversation.conversationId conversationType:model.conversation.type];
//   viewController.title = model.title;
    viewController.title = mingYiModel.docName;
    [self.navigationController pushViewController:viewController animated:YES];  
}

###分析需求(二)

第二个界面
这个界面的UI大部分都已经有了,需要改的是导航控制器标题、昵称、头像。 ###实现相关功能(二) 1.标题在上一个界面调用跳转聊天控制器EaseMessageViewController的时候就已经传过来了。

2.关于昵称跟头像,需要修改环信的消息扩展类。消息扩展类在发送各种消息的时候都要设置。(文字、语音、表情、图片、视频)

昵称跟头像URL

发送语音增加消息扩展类

发送视频增加消息扩展类

发送文本消息增加消息扩展类

发送表情增加消息扩展类

最后需要用到的地方,在EaseMessageViewController.m- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}方法的最后加上如下代码:

注意字典里的关键字要跟前面设置的一样
如果URL中没有头像,就需要设置默认头像。如下

没有头像就加载头像
需要注意的是,字典中值不能为空,要考虑各种情况,避免字典为空。还要注意的是,如果项目既有iOS版本,又有安卓版本,字典中键字符串需要保持一致。用户的昵称是本地存的,头像是我服务器存的,URL也是我服务器给的。 ###分析需求(三)

第三个界面
这个界面的需求就是获取环信好友列表,并按首字母进行排序。昵称跟头像同样需要自己处理。 ###实现相关功能(三) 1.获取环信好友列表。

 //------------------从环信服务器获取所有的好友 异步加载-------------------//
    [[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
        if (!aError) {
            
            self.dataArr = [[NSMutableArray alloc]initWithArray:aList];
            
            NSLog(@"haoyou==%ld",self.dataArr.count);
        
            //-------主线程刷新UI---------
            [self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
        }
    }];

2.将获取到的环信ID跟本地数据库匹配,获取中文昵称。

//1. 利用好友账号在数据库中检索,好友模型
    SaveMingYiModel *model = [[SaveMingYiModel alloc]init];
    self.modelArr = [[NSMutableArray alloc]init];
    for (NSString *str in self.dataArr) {
        
        model = [HaoYouDao selectedFromPeopleTableWithMDsfz:str];
        if (model) {
            [self.modelArr addObject:model];
        }
        
    }
    //2. 通过好友模型,输出好友名字,并添加到数组中
    NSLog(@"%ld",self.dataArr.count);
    if (self.modelArr.count>0) {
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        for (SaveMingYiModel *model in self.modelArr) {
            
            NSString *name = model.docName;
            if (name) {
                [arr addObject:name];
            }
        }
        
    //3. 将汉字转换为拼音,并进行排序
    self.indexArray      = [ChineseString IndexArray:arr];
    self.letterResultArr = [ChineseString LetterSortArray:arr];
    [self.tableView reloadData];

关于按拼音首字母排序,需要导入两个类文件ChineseStringpinyin,网上可以搜到。需要的话也可以留邮箱,我发过去。下面贴上这个界面的全部代码。有些是关于本地数据库的类,关于数据库的操作这里不写。

#import "HaoYouViewController.h"
#import "HaoYouTableViewCell.h"

#import "EMSDK.h"
#import "EaseUI.h"//环信相关
#import "ChineseString.h"//按照拼音首字母排序
#import "MyDoctorItem.h"//好友模型
#import "FDLoginTool.h"//登录工具类
#import "HaoYouDao.h"//数据库DAO类
#import "SaveMingYiModel.h"//数据库相关的好友模型
#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
@interface HaoYouViewController ()<UITableViewDelegate,UITableViewDataSource,EMChatManagerDelegate,EMContactManagerDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) NSMutableArray *modelArr;
@property (nonatomic,strong) NSMutableArray *indexArray;
@property (nonatomic,strong) NSMutableArray *letterResultArr;
@property (nonatomic,strong) UILabel *sectionTitleView;
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation HaoYouViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"医生列表";
    self.view.backgroundColor = kGlobalBackgroundColor;
    
    // 打开数据库
    [HaoYouDao createYiShengTable];
    // 创建表
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight  = 65;
    
    [self.view addSubview:_tableView];
    //去掉多余的分割线
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    //改变索引字的颜色,索引背景的颜色
    self.tableView.sectionIndexColor =[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    
    //------------------从环信服务器获取所有的好友 异步加载-------------------//
    [[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
        if (!aError) {
            
            self.dataArr = [[NSMutableArray alloc]initWithArray:aList];
            
            NSLog(@"haoyou==%ld",self.dataArr.count);
            
            
            //-------主线程刷新UI---------
            [self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
        }
    }];

    
}

/*!
 @method  刷新UI。
 @abstract 加载好友列表,按首字母排序.
 
 */
- (void)refreshUI
{
    self.sectionTitleView = ({
        UILabel *sectionTitleView = [[UILabel alloc] initWithFrame:CGRectMake((APP_WIDTH-100)/2, (APP_HEIGHT-100)/2,100,100)];
        sectionTitleView.textAlignment = NSTextAlignmentCenter;
        sectionTitleView.font          = [UIFont boldSystemFontOfSize:60];
        sectionTitleView.textColor     = [UIColor grayColor];
        sectionTitleView.backgroundColor     = [UIColor whiteColor];
        sectionTitleView.layer.cornerRadius  = 6;
        sectionTitleView.layer.masksToBounds = YES;
        sectionTitleView.layer.borderWidth   = 1.f/[UIScreen mainScreen].scale;
        _sectionTitleView.layer.borderColor  = [UIColor groupTableViewBackgroundColor].CGColor;
        sectionTitleView;
    });
    [self.navigationController.view addSubview:self.sectionTitleView];
    self.sectionTitleView.hidden = YES;

    //1. 利用好友账号在数据库中检索,好友模型
    SaveMingYiModel *model = [[SaveMingYiModel alloc]init];
    self.modelArr = [[NSMutableArray alloc]init];
    for (NSString *str in self.dataArr) {
        
        model = [HaoYouDao selectedFromPeopleTableWithMDsfz:str];
        if (model) {
            [self.modelArr addObject:model];
        }
        
    }
    //2. 通过好友模型,输出好友名字,并添加到数组中
    NSLog(@"%ld",self.dataArr.count);
    if (self.modelArr.count>0) {
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        for (SaveMingYiModel *model in self.modelArr) {
            
            NSString *name = model.docName;
            if (name) {
                [arr addObject:name];
            }
        }
        
    //3. 将汉字转换为拼音,并进行排序
    self.indexArray      = [ChineseString IndexArray:arr];
    self.letterResultArr = [ChineseString LetterSortArray:arr];
    [self.tableView reloadData];

    }
}
//------------------tableview 的delegate,dataSource 方法--------------//

// 返回区头索引数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.indexArray;
}
// 返回区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString *key = [self.indexArray objectAtIndex:section];
    return key;
}
// 返回区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexArray count];
}
// 返回每区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.letterResultArr objectAtIndex:section] count];
}
// 配置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HaoYouTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"HaoYouTableViewCell" owner:nil options:nil]lastObject];
    
    cell.nameLabel.text = [[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    
    SaveMingYiModel *model = [HaoYouDao selectedFromPeopleTableWithName:[[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
    
    //将小写x转换为大写X
    NSString *upStr = [model.idcardno uppercaseString];
    NSLog(@"sss=%@",upStr);
    //拼接头像URL地址
    NSString *imgUrl = [@"https://219.140.163.100:2265/FDAPP/app_upload/" stringByAppendingFormat:@"%@.jpg",upStr];
    
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
    
    
    
    //统一设置单元格分割线,和单元格选中样式
    cell.layoutMargins  = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;

}

// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.indexArray.count>0) {
        
        return 50;
    }
    else
    {
        return kScreenH - 64;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    [self showSectionTitle:title];
    return index;
}

#pragma mark - private
- (void)timerHandler:(NSTimer *)sender
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.3 animations:^{
            self.sectionTitleView.alpha = 0;
        } completion:^(BOOL finished) {
            self.sectionTitleView.hidden = YES;
        }];
    });
}

-(void)showSectionTitle:(NSString*)title{
    [self.sectionTitleView setText:title];
    self.sectionTitleView.hidden = NO;
    self.sectionTitleView.alpha  = 1;
    [self.timer invalidate];
    self.timer = nil;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerHandler:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

#pragma mark - UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 25)];
    headerView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 40, 25)];
    lab.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    
    lab.font = [UIFont systemFontOfSize:12];
    lab.text = [self.indexArray objectAtIndex:section];
    lab.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    [headerView addSubview:lab];
    return headerView;
}

// 点击好友,进入详情界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 找到好友模型
    SaveMingYiModel *model = [HaoYouDao selectedFromPeopleTableWithName:[[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
    // 根据好友的身份证MD5 跳转聊天界面
    EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:model.sfzMD5 conversationType:EMConversationTypeChat];
    // 隐藏底部tabBar
    chatController.hidesBottomBarWhenPushed = YES;
    // 聊天标题
    chatController.title = model.docName;
    [self.navigationController pushViewController:chatController animated:YES];
}
// 设置区头区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.title = @"医生列表";
    
}

至此,聊天功能大部分写完,下面贴一张本地数据库的图。

关键信息要打码~
环信ID要的是数字跟字母,所以在项目中,我就用身份证MD5加密作为环信的ID,数据库中每条好友信息都是从服务器中请求的,存入数据库,方便通过mdSfz(也就是通过环信请求到的好友ID)这个字段去数据库检索信息。 ###最后说一下消息推送部分 1.远程推送。之前在AppDelegate中的设置如果没有什么问题,并且推送证书 p12文件都是正确的话,(p12文件要上传到环信后台)远程推送就没有任何问题。

2.本地推送。在MainViewController.m中处理,(进入程序的第一个视图控制器)首先导入头文件EaseSDKHelper.hviewDidLoad中注册聊天模块监听 [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];然后写入如下代码:

/*!
 @method  环信方法,监听收到消息
 @abstract 不管app前台、后台,收到消息都会来到这个方法
 */
- (void)messagesDidReceive:(NSArray *)aMessages
{
    // 前台就不跳转
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
        // 震动
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        
    }
    
    EMMessage *message = aMessages[0];
    
    //获得消息体
    EMMessageBody *messageBody = message.body;
    
    NSString *str;
    if (messageBody.type==EMMessageBodyTypeText) {
        //消息为文本时,强转为EMTextMessageBody 获取文本消息
        EMTextMessageBody *textBody =(EMTextMessageBody *)messageBody;
        str = textBody.text;
        
    }
    //判断消息类型,输出相应类型
    else if (messageBody.type==EMMessageBodyTypeImage)
        
    {
        str = @"[图片]";
        
    }
    else if (messageBody.type==EMMessageBodyTypeVoice)
    {
        str = @"[语音]";
        
    }
    else if (messageBody.type==EMMessageBodyTypeLocation)
    {
        str = @"[位置]";
        
    }
    else if (messageBody.type==EMMessageBodyTypeVideo)
    {
        str = @"[视频]";
        
    }
    else if (messageBody.type==EMMessageBodyTypeFile)
    {
        str = @"[文件]";
    }
    else
    {
        str = @"";
    }
    
    //message.ext[@"img"];//头像
    //message.ext[@"accountName"];//昵称
    
    NSString *notifyMessage = [NSString stringWithFormat:@"%@:%@",message.ext[@"accountName"],str];
    NSLog(@"notifyMessage%@",notifyMessage);
    
    UILocalNotification *notify = [[UILocalNotification alloc]init];
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    notify.fireDate = fireDate;
    
    //时区
    notify.timeZone = [NSTimeZone defaultTimeZone];
    
    //通知内容
    notify.alertBody = notifyMessage;
    
    //通知被触发时播放的声音
    notify.soundName = UILocalNotificationDefaultSoundName;
    //通知参数
    
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:message.ext[@"accountName"],@"好友",@(badgeNum),@"badgeNum",nil];
    
    notify.userInfo = dic;
    
    // ios8后,需要添加这个注册,才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             // 执行通知注册
            [[UIApplication sharedApplication] scheduleLocalNotification:notify];
        });
    }
}

本地通知发送完成,若需要本地通知相关信息,需要在AppDelegate.m中接收本地通知并处理。

本地推送


基于环信实现实时语音视频通话功能

转载请注明出处 © XDChang