博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS--环信集成并修改头像和昵称(需要自己的服务器)
阅读量:6293 次
发布时间:2019-06-22

本文共 6715 字,大约阅读时间需要 22 分钟。

hot3.png

 

一、环信集成

使用Cocopods导入:

   pod 'EaseUI', :git => 'https://github.com/easemob/easeui-ios-hyphenate-cocoapods.git'

   pod 'Hyphenate'

二、初始化SDK

 //AppKey:注册的AppKey,详细见下面注释。

    //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。
    EMOptions *options = [EMOptions optionsWithAppkey:IMAPPKEY];
    options.apnsCertName = @"istore_dev";
    [[EMClient sharedClient] initializeSDKWithOptions:options];

//初始化EaseUI (可以与初始化SDK二选一,本人都写了,以防万一)

    [[EaseSDKHelper shareHelper] hyphenateApplication:application 

 didFinishLaunchingWithOptions:launchOptions appkey:IMAPPKEY  apnsCertName:@"qy"

  otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];

三、环信登录 

在登录环信之前要登录自己的APP,这样后台要和环信后台交互(可以让后台自动注册环信),在APP登录成功时返回用户名和密码,用于后期的环信登录

 //登录环信

    EMError *error1 = [[EMClient sharedClient] loginWithUsername:[[NSUserDefaults standardUserDefaults] objectForKey:@"uuid"]  password:[[NSUserDefaults standardUserDefaults] objectForKey:@"password"]];

NSLog(@"ppppppperror1:%d",error1.code);

//error1.code=0,则登录成功

 //自动登录

 [[EMClient sharedClient].options setIsAutoLogin:YES];

代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    //AppKey:注册的AppKey,详细见下面注释。    //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。    EMOptions *options = [EMOptions optionsWithAppkey:@"douser#istore"];    options.apnsCertName = @"istore_dev";    [[EMClient sharedClient] initializeSDKWithOptions:options];    //环信 IMAPPKEY    //AppKey:注册的AppKey,详细见下面注释。    //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。        EMOptions *options = [EMOptions optionsWithAppkey:IMAPPKEY];    options.apnsCertName = @"qy";    [[EMClient sharedClient] initializeSDKWithOptions:options];        //登录环信    EMError *error1 = [[EMClient sharedClient] loginWithUsername:[[NSUserDefaults  standardUserDefaults] objectForKey:@"uuid"]  password:[[NSUserDefaults standardUserDefaults] objectForKey:@"password"]];         NSLog(@"ppppppperror1:%d",error1.code);    //自动登录    [[EMClient sharedClient].options setIsAutoLogin:YES];        //初始化EaseUI    [[EaseSDKHelper shareHelper] hyphenateApplication:application                        didFinishLaunchingWithOptions:launchOptions                                               appkey:IMAPPKEY                                         apnsCertName:@"qy"                                          otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];        return YES;}// APP进入后台- (void)applicationDidEnterBackground:(UIApplication *)application{    [[EMClient sharedClient] applicationDidEnterBackground:application];}// APP将要从后台返回- (void)applicationWillEnterForeground:(UIApplication *)application{    [[EMClient sharedClient] applicationWillEnterForeground:application];}

四、初始化聊天页面

可以自己创建一个ViewController并且继承 EaseMessageViewController,这样方便后期修改头像

可以与后台沟通好,创建群成功时直接让后台返回群ID即可

//群ID:@"groupId"//聊天类型:EMConversationTypeGroupChat HYNChatViewController *chatController = [[HYNChatViewController alloc] initWithConversationChatter:@"groupId" conversationType:EMConversationTypeGroupChat];chatController.title = self.navigationItem.title;//聊天界面的标题[self.navigationController pushViewController:chatController animated:YES];

初始化单聊界面

//个人ID:@"groupId"//聊天类型: EMConversationTypeChatHYNChatViewController *chatController = [[HYNChatViewController alloc] initWithConversationChatter:conver.conversationId conversationType:EMConversationTypeChat];chatController.title = self.navigationItem.title;//聊天界面的标题[self.navigationController pushViewController:chatController animated:YES];;

获取会话列表

//getConversation: 获取或创建与8001的会话//type:EMConversationTypeChat: 会话类型EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES];//获取所有会话NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];//获取会话未读消息数[EMConversation unreadMessagesCount];

显示用户昵称和头像

主要在这个方法进行修改- (id
)messageViewController:(EaseMessageViewController *)viewController modelForMessage:(EMMessage *)message;

五、展示有昵称和头像的聊天界面

1.在列表页面解析数据,点击行时传入所需要的数据如:昵称和头像

列表页面//获取所有会话列表_conversations  = [[EMClient sharedClient].chatManager getAllConversations];//拿到所有会话列表的conversationId,用于解析数据 NSMutableArray *arr = [NSMutableArray new];        for(int i=0; i<_conversations.count; i++){                EMConversation *conversation = _conversations[i];        [arr addObject:conversation.conversationId];                NSLog(@"conversationId:%@",conversation.conversationId);            }//数据解析    //点击行单元时,传入数据并跳转单聊界面EMConversation *conver = _conversations[indexPath.row];     CircleFansListModel *model = _userinfoModelArr[indexPath.row];HYNChatViewController *chatController = [[HYNChatViewController alloc] initWithConversationChatter:conver.conversationId conversationType:EMConversationTypeChat];        chatController.title = model.nickname;        chatController.nickname = model.nickname;        chatController.avatarURLPath = model.headimg; [self.navigationController pushViewController:chatController animated:YES];

在继承了EaseMessageViewController的单聊界面

.h中

#import 
//要遵守协议EaseMessageViewControllerDelegate,EaseMessageViewControllerDataSource@interface HYNChatViewController : EaseMessageViewController
@property (nonatomic, strong) NSString *chatWay;//chatWay=0,圈聊,chatWay=1,私信@property (nonatomic, copy) NSString *nickname;@property (nonatomic, copy) NSString *avatarURLPath;@end

.m中

- (void)viewDidLoad {    [super viewDidLoad];   //一定要加上这俩个代理设置,之前因为没有添加,找了半天的bug,最后发现原来是这里忘记设置了,简直吐了2升血    self.delegate = self;    self.dataSource = self;     //刷新     self.showRefreshHeader = YES;}//修改头像和昵称的方法#pragma mark - EaseMessageViewControllerDataSource- (id
)messageViewController:(EaseMessageViewController *)viewController modelForMessage:(EMMessage *)message{ id
model = nil; model = [[EaseMessageModel alloc] initWithMessage:message]; NSLog(@"-----message:%@",message); if (model.isSender) {//自己发送 ,头像和昵称是在登录时后台返回的 model.message.ext = @{@"avatar":[[NSUserDefaults standardUserDefaults] objectForKey:@"headimgurl"],@"nickname":[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"]}; //头像 model.avatarURLPath = [[NSUserDefaults standardUserDefaults] objectForKey:@"headimgurl"]; //昵称 model.nickname = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"]; //头像占位图 model.failImageName = @"logo"; }else{//对方发送 //列表页面的传值 //头像 model.avatarURLPath = _avatarURLPath; //昵称 model.nickname = _nickname; //头像占位图 model.failImageName = @"logo"; } return model;}

最后展示一下结果

 

有不足之处请留言,谢谢!

 

 

转载于:https://my.oschina.net/huangyn/blog/1359541

你可能感兴趣的文章
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
Git 常用命令
查看>>
驰骋工作流引擎三种项目集成开发模式
查看>>
SUSE11修改主机名方法
查看>>
jdk6.0 + Tomcat6.0的简单jsp,Servlet,javabean的调试
查看>>
Android:apk签名
查看>>
2(2).选择排序_冒泡(双向循环链表)
查看>>
MySQL 索引 BST树、B树、B+树、B*树
查看>>
微信支付
查看>>
CodeBlocks中的OpenGL
查看>>
短址(short URL)
查看>>
第十三章 RememberMe——《跟我学Shiro》
查看>>
mysql 时间函数 时间戳转为日期
查看>>
索引失效 ORA-01502
查看>>
Oracle取月份,不带前面的0
查看>>
Linux Network Device Name issue
查看>>
IP地址的划分实例解答
查看>>
如何查看Linux命令源码
查看>>
运维基础命令
查看>>