基于环信Demo3.0,实现单聊功能

注意:

  1. 这里Demo集成的是带有实时语音功能的(libEaseMobClientSDK.a)。
  2. 环信库是直接拖拽EaseMobSDK文件夹到项目内的,没有使用pod管理。

最终效果

15

添加环信SDK

  1. 下载环信SDK 2.2.1
    链接地址
    1
  2. 添加EaseMobSDK到项目中,并添加依赖库。这里需要注意的地方:libEaseMobClientSDKLite.a不包含实时语音功能,libEaseMobClientSDK.a包含所有功能。必须要删掉一个,否则会报错。这里删除libEaseMobClientSDKLite.a。
  3. 添加依赖库。参见官方文档
    2

    3
  4. 新创建一个pch,并在Build Settings中添加配置。
    在pch中,导入头文件。
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "EaseMob.h"
#endif /* PrefixHeader_pch */ 
4

添加环信EaseUI

  1. 导入EaseUI到项目中,并实现基础的登陆功能。
    找到Demo3.0的EaseUI目录,然后将如下文件,拖拽到项目中。并在pch文件中引入EaseUI.h
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "EaseMob.h"
#import "EaseUI.h"
#endif /* PrefixHeader_pch */
5

6
  1. 在AppDelegate.m中注册环信的SDK和UI。
  2. 在StoryBoard中新增一个按钮,并给予登陆方法。然后在方法中实现登陆功能。当登陆成功后,发送通知。收到通知后,跳转到UI提供的EaseMessageViewController界面。

其中APPDelegate中文件内容

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //注册SDK
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"easemob-demo#chatdemoui" apnsCertName:nil];
    //注册UI
    [[EaseSDKHelper shareHelper] easemobApplication:application
                      didFinishLaunchingWithOptions:launchOptions
                                             appkey:@"easemob-demo#chatdemoui"
                                       apnsCertName:nil
                                        otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

ViewController中的内容



- (IBAction)login:(id)sender {
    //用户名:zlanchun 密码:123456
    //异步登陆账号
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:@"zlanchun" password:@"123456" completion:^(NSDictionary *loginInfo, EMError *error) {
         if (loginInfo && !error) {
             //发送自动登陆状态通知
             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
         } else {
             NSLog(@"login error: %@",error);
         }
     } onQueue:nil];
}

- (void)jumpToChatVC {
    //创建聊天室 对象:zlanchun1
    EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"zlanchun1" conversationType:eConversationTypeChat];
    [self.navigationController pushViewController:chatController animated:YES];
}

继承Demo3.0中的功能

  1. 首先导入Demo3.0中如下文件到项目中:


    7
  2. 导入class里单聊功能ChatView到项目中


    8
  3. 导入完成后的项目目录


    9
  4. 添加头文件到PCH中。
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
//头文件
#import "ChatDemoUIDefine.h"
#import "EMAlertView.h"
#import "TTGlobalUICommon.h"
#import "UIViewController+DismissKeyboard.h"
#import "NSString+Valid.h"
#import "EaseMob.h"
#import "EaseUI.h"
#endif /* PrefixHeader_pch */
  1. 注释未引用的文件,删除重复引用的文件,添加第三方库Parse的依赖。
    注释未引用的文件:

  2. 在ViewController.m中应用头文件#import "ChatViewController.h"。然后在跳转方法中,注册跳转VC,跳转即可。

ViewController.m新增部分:

- (void)jumpToChatVC {
    //创建聊天室 对象:zlanchun1
    //EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"zlanchun1" conversationType:eConversationTypeChat];
    //创建基于Demo3.0Class的chatroom
    ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:@"zlanchun1" conversationType:eConversationTypeChat];
    [self.navigationController pushViewController:chatController animated:YES];
}

ChatViewController.m中需要注释的地方

#import "ChatViewController.h"
//#import "ChatGroupDetailViewController.h"
//#import "ChatroomDetailViewController.h"
#import "CustomMessageCell.h"
//#import "UserProfileViewController.h"
//#import "UserProfileManager.h"
//#import "ContactListSelectViewController.h"
- (void)messageViewController:(EaseMessageViewController *)viewController
   didSelectAvatarMessageModel:(id<IMessageModel>)messageModel
{
    //UserProfileViewController *userprofile = [[UserProfileViewController alloc] initWithUsername:messageModel.nickname];
    //[self.navigationController pushViewController:userprofile animated:YES];
}
- (id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController
                           modelForMessage:(EMMessage *)message
{
    id<IMessageModel> model = nil;
    model = [[EaseMessageModel alloc] initWithMessage:message];
    model.avatarImage = [UIImage imageNamed:@"EaseUIResource.bundle/user"];
//    UserProfileEntity *profileEntity = [[UserProfileManager sharedInstance] getUserProfileByUsername:model.nickname];
//    if (profileEntity) {
//        model.avatarURLPath = profileEntity.imageUrl;
//    }
    model.failImageName = @"imageDownloadFail";
    return model;
}
- (void)showGroupDetailAction
{
    [self.view endEditing:YES];
//    if (self.conversation.conversationType == eConversationTypeGroupChat) {
//        ChatGroupDetailViewController *detailController = [[ChatGroupDetailViewController alloc] initWithGroupId:self.conversation.chatter];
//        [self.navigationController pushViewController:detailController animated:YES];
//    }
//    else if (self.conversation.conversationType == eConversationTypeChatRoom)
//    {
//        ChatroomDetailViewController *detailController = [[ChatroomDetailViewController alloc] initWithChatroomId:self.conversation.chatter];
//        [self.navigationController pushViewController:detailController animated:YES];
//    }
}
- (void)transpondMenuAction:(id)sender
{
//    if (self.menuIndexPath && self.menuIndexPath.row > 0) {
//        id<IMessageModel> model = [self.dataArray objectAtIndex:self.menuIndexPath.row];
//        ContactListSelectViewController *listViewController = [[ContactListSelectViewController alloc] initWithNibName:nil bundle:nil];
//        listViewController.messageModel = model;
//        [listViewController tableViewDidTriggerHeaderRefresh];
//        [self.navigationController pushViewController:listViewController animated:YES];
//    }
    self.menuIndexPath = nil;
}

UIImageView+HeadImage.文件中注释

/************************************************************
 *  * EaseMob CONFIDENTIAL
 * __________________
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of EaseMob Technologies.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from EaseMob Technologies.
 */


#import "UIImageView+HeadImage.h"
//#import "UserProfileManager.h"

@implementation UIImageView (HeadImage)

- (void)imageWithUsername:(NSString *)username placeholderImage:(UIImage*)placeholderImage
{
    if (placeholderImage == nil) {
        placeholderImage = [UIImage imageNamed:@"chatListCellHead"];
    }
//    UserProfileEntity *profileEntity = [[UserProfileManager sharedInstance] getUserProfileByUsername:username];
//    if (profileEntity) {
//        [self sd_setImageWithURL:[NSURL URLWithString:profileEntity.imageUrl] placeholderImage:placeholderImage];
//    } else {
//        [self sd_setImageWithURL:nil placeholderImage:placeholderImage];
//    }
    [self sd_setImageWithURL:nil placeholderImage:placeholderImage];
}

@end

@implementation UILabel (Prase)

- (void)setTextWithUsername:(NSString *)username
{
//    UserProfileEntity *profileEntity = [[UserProfileManager sharedInstance] getUserProfileByUsername:username];
//    if (profileEntity) {
//        if (profileEntity.nickname && profileEntity.nickname.length > 0) {
//            [self setText:profileEntity.nickname];
//            [self setNeedsLayout];
//        } else {
//            [self setText:username];
//        }
//    } else {
//        [self setText:username];
//    }
    [self setText:username];
}
@end

添加parse依赖库:

  • StoreKit.framework
  • Bolts.framework
  • Parse.framework
  • Accounts.framework
  • Social.framework。
    其中Bolts.framework/Parse.framework在文件夹EaseClass/3rdparty/Parse下。


    10

    11

删除重用引用MBProgressHUD库


12

删除wav.mm文件。


13

删除VoiceConvert文件


14

错误集合

  • 报错1:未添加Parse依赖库引起的clang: error: linker command failed with exit code 1 (use -v to see invocation)错误。

    16

    解决办法:添加parse的依赖库,一共有5个(环信小哥告诉我有4个,结果,添加完4个依赖后,9个错误变成4个了。咋办?还好有Google,查parse依赖关系,发现15年7月份依赖增加了一个。)
    剩余的4个错误("_OBJC_CLASS_$_SKPayment"):
    17

    看看国外小哥怎么说得(地址)。
    18

  • 报错2:删除重复引用MBProgressHUD库


    19
  • 报错3:删除重复引用wav.mm文件


    20
  • 报错4:删除VoiceConvert文件


    21

Demo程序见:这里(百度云盘)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容