基于环信Demo3.0,实现单聊功能 -- 简书内容

http://www.jianshu.com/p/f53be9664f14

注意:

  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
  2. 在AppDelegate.m中注册环信的SDK和UI。
  3. 在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 */
  5. 注释未引用的文件,删除重复引用的文件,添加第三方库Parse的依赖。
    注释未引用的文件:

  6. 在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程序见:这里(百度云盘)


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"C语言字符串处理库函数大全-简书"是一篇在简书上的教程,介绍了C语言中常用的字符串处理库函数。 该教程详细介绍了C语言中字符串操作的相关函数,包括库函数的使用方法和示例代码。通过该教程,读者可以学习到如何使用C语言中的字符串处理函数来进行字符串的复制、连接、比较、查找、截取等操作。 在这篇教程中,读者可以了解到以下一些常见的字符串处理库函数: 1. strcpy:用于将一个字符串复制到另一个字符串中。 2. strcat:用于将一个字符串连接到另一个字符串的末尾。 3. strlen:用于计算一个字符串的长度。 4. strcmp:用于比较两个字符串的大小。 5. strchr:用于在一个字符串中查找指定字符的第一次出现位置。 6. strstr:用于在一个字符串中查找指定子串的第一次出现位置。 7. strtok:用于将一个字符串按照指定的分隔符分割成多个子串。 8. strncpy:用于将指定长度的字符串复制到目标字符串中。 9. strncmp:用于比较指定长度的两个字符串。 10. sprintf:用于将格式化的字符串输出到一个字符数组中。 这些函数在C语言中非常常用,并且对于字符串的处理非常方便。通过学习和掌握这些字符串处理库函数,可以更加高效地完成C语言程序中的字符串操作。 总之,"C语言字符串处理库函数大全-简书"这篇文章提供了丰富的字符串处理库函数以及示例代码,对于C语言开发者来说是一个很好的参考和学习资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值