在上一篇文章中咱们了解了环信客户互动云的功能,以及集成SDK前须要作的配置工做,接下来咱们正式进入项目集成阶段,集成流程摘自环信CEC iOS SDK集成,本文对环信文档作了更加详细的说明,以及一些未指明错误的处理办法,若有不当之处,请各位大神指正. 文末会附上GitHub的demo地址 #1、集成ios
提示 : 请按照如下5步所有操做完毕后再编译,中间步骤执行中若是编译报错了不用管,继续往下执行便可git
首先咱们先新建本身的项目,名称起为HuanxinKefugithub
打开项目目录,新建一个文件夹HXService,从商城Demo中找HelpDeskUI文件夹,找到后复制进HXService文件夹bash
新建一个文件夹HXFramework,而后将HelpDesk.framework、HyphenateLite.framework(不包含实时语音功能)放在HXFramework文件夹中app
注意点 : Xcode 9.0的bug,把文件拖进项目后,这些文件的.m不能自动添加到Compile Source中,致使找不到文件等错误,须要手动添加.m到Compile Source,特别麻烦 解决方案 : 升级Xcode 9.1或9.2ui
HFileViewController : 查看聊天文件控制器(不是发送文件的) HDChatViewController : 聊天界面控制器(最主要的控制器) AppDelegate+HelpDesk : 初始化环信SDK,APNs,登陆,退出操做,多设备登陆功能等 SCLoginManger : 环信SDK管理者(单例) SVProgressHUD : 蒙版/HUD LocalDefine : 一些常量的宏定义 spa
//如下两段代码选用一个就好
//包含实时音视频的这样添加
#ifdef __OBJC__
#import <HelpDesk/HelpDesk.h>
#import "HelpDeskUI.h"
#import "SVProgressHUD.h"
#import "LocalDefine.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "其余头文件"
#endif
//不包含实时音视频的这样添加
#ifdef __OBJC__
#import <HelpDeskLite/HelpDeskLite.h>
#import "HelpDeskUI.h"
#import "SVProgressHUD.h"
#import "LocalDefine.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "其余头文件"
#endif
复制代码
注意点:若是你的pch文件中已经导入了其余头文件,务必将其余头文件也写在#ifdef __OBJC__ 和 #endif 之间,否则会报错,由于SDK中包含的有C文件.net
//#import <Bugly/Bugly.h>
//[Bugly startWithAppId:@"b336efe49a"];
复制代码
#import "SCLoginManager.h"
复制代码
//第17行导入头文件,注释掉
//#import "HDLeaveMsgViewController.h"
复制代码
//第77行的留言方法注释掉
// 留言
/*
- (void)moreViewLeaveMessageAction:(HDChatBarMoreView *)moreView
{
[self stopAudioPlayingWithChangeCategory:YES];
HDLeaveMsgViewController *leaveMsgVC = [[HDLeaveMsgViewController alloc] init];
[self.navigationController pushViewController:leaveMsgVC animated:YES];
}
*/
复制代码
//第211行,跳转留言控制器的方法注释掉
/*
- (void)didPressedLeaveMsgButton {
HDLeaveMsgViewController *leaveMsgVC = [[HDLeaveMsgViewController alloc] init];
[self.navigationController pushViewController:leaveMsgVC animated:YES];
}
*/
复制代码
若是导入SDK后,编译,报文件重复duplicate的错,那是由于环信HelpDeskUI中的第三方库与你项目中已集成的第三方库重复,因此直接删掉HelpDeskUI--3rdparty中对应的库便可3d
Privacy - Photo Library Usage Description 须要访问您的相册 Privacy - Microphone Usage Description 须要访问您的麦克风 Privacy - Camera Usage Description 须要访问您的摄像机 代理
#import "AppDelegate+HelpDesk.h"
#import "ViewController.h" //初始界面,登陆/注册界面
复制代码
//1.建立窗口
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//2.初始化环信
[self easemobApplication:application didFinishLaunchingWithOptions:launchOptions];
//3.添加自定义小表情
[[HDEmotionEscape sharedInstance] setEaseEmotionEscapePattern:@"\\[[^\\[\\]]{1,3}\\]"];
[[HDEmotionEscape sharedInstance] setEaseEmotionEscapeDictionary:[HDConvertToCommonEmoticonsHelper emotionsDictionary]];
//4.设置根控制器
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [story instantiateViewControllerWithIdentifier:@"story"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
//5.显示窗口
[self.window makeKeyAndVisible];
return YES;
复制代码
#import "HDChatViewController.h"
#import "SCLoginManager.h"
复制代码
而后实现注册方法
//注册
- (void)regHuanXin{
HError *error = [[HChatClient sharedClient] registerWithUsername:self.username.text password:self.password.text];
if (!error) {
NSLog(@"注册成功");
}else{
NSLog(@"注册失败:%@",error.errorDescription);
}
}
复制代码
实现登陆方法,判断登陆成功,则跳转到聊天控制器HDChatViewController
//登陆方法
- (void)loginHuanXin{
HChatClient *client = [HChatClient sharedClient];
if (client.isLoggedInBefore != YES) {
HError *error = [client loginWithUsername:self.username.text password:self.password.text];
if (!error) {
NSLog(@"登陆成功");
[self joinChat];
} else {
NSLog(@"登陆失败");
}
}else{
[self joinChat];
}
}
复制代码
//跳转到聊天控制器(重点)
- (void)joinChat{
SCLoginManager *sc = [SCLoginManager shareLoginManager];
HDChatViewController *chatVC = [[HDChatViewController alloc] initWithConversationChatter:sc.cname]; // 获取地址:kefu.easemob.com,“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
[self.navigationController pushViewController:chatVC animated:YES];
}
复制代码
//退出登陆
//若是要更换帐号登陆,必定要退出登陆后再登陆
- (IBAction)loginOutAction:(id)sender {
HError *error = [[HChatClient sharedClient] logout:YES];
if (error) { //登出出错
NSLog(@"退出登陆出错");
} else {//登出成功
NSLog(@"退出登陆成功");
}
}
复制代码
找到AppDelegate+HelpDesk.m文件中的initializeCustomerServiceSdk方法,配置推送证书的名称,关于生成推送证书可参考 : www.jianshu.com/p/4be583833… 其中的选项能够关闭/开启打印日志
在LocalDefine.h文件中,填写与客户互动云后台关联的信息,与后台创建关联关系
至此,基本集成工做已所有完毕,咱们打开APP尝试聊天 首先注册帐号
//添加消息监控,第二个参数是执行代理方法的队列,默认是主队列
[[HChatClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
//移除消息监控
[[HChatClient sharedClient].chatManager removeDelegate:self];
- (void)messagesDidReceive:(NSArray *)aMessages{
//收到普通消息,格式:<HMessage *>
//只接收未读消息,每条消息都会调用一次该方法
}
复制代码
1.更多扩展功能能够查看环信文档,若有须要,我能够将功能加在本身的Demo中
2.若是在集成过程当中遇到任何问题,请在下方留言~咱们共同交流
3.附github的demo地址 : 本身写的demo 下载后只需导入HelpDeskLite.framework,HyphenateLite.framework便可
4.感谢各位的支持~