iOS--环信集成并修改头像和昵称(需要自己的服务器)
iOS--环信集成并修改头像和昵称(需要自己的服务器)
指尖流年 发表于5个月前
iOS--环信集成并修改头像和昵称(需要自己的服务器)
  • 发表于 5个月前
  • 阅读 131
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 需要自己的服务器

 

一、环信集成

使用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"
//聊天类型: EMConversationTypeChat
HYNChatViewController *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<IMessageModel>)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 <EaseUI/EaseUI.h>

//要遵守协议EaseMessageViewControllerDelegate,EaseMessageViewControllerDataSource
@interface HYNChatViewController : EaseMessageViewController<EMChatManagerDelegate,EaseMessageViewControllerDelegate,EaseMessageViewControllerDataSource>

@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<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController modelForMessage:(EMMessage *)message
{
    id<IMessageModel> 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;
}

最后展示一下结果

 

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

 

 

共有 人打赏支持
粉丝 4
博文 48
码字总数 19224
×
指尖流年
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: