有关环信的导入,官方文档说的已经很详细了,这里我就不去重复了,导入之后也许会报错,要检查自己是不是重复导入的三方或者少导入环信的系统库,像md,mj这些三方环信的官方demo都用了.最开始要一步一步来,别一下导入太多,到时候错误不好重现.
首先,我的项目不需要修改聊天界面,就直接把环信的ChatUI拿来用了,聊天列表页不满意的可以自己写.首先要做的就是用户的头像和昵称要保存到自己的服务器
写个类通过环信的im_id去保存对应的信息
在pch文件中加入通知.pch中导入的类名要写在#ifdef __OBJC__ #endif中
/** IM通知*/
#define HMSortDidSelectNotification @"HMSortDidSelectNotification"
#define HMSelectedSort @"HMSelectedSort"
#define HMCityDidSelectNotification @"HMCityDidSelectNotification"
#define HMSelectedCity @"HMSelectedCity"
#define HMCategoryDidSelectNotification @"HMCategoryDidSelectNotification"
#define HMSelectedCategory @"HMSelectedCategory"
#define HMSelectedSubCategoryName @"HMSelectedSubCategoryName"
#define HMRegionDidSelectNotification @"HMRegionDidSelectNotification"
#define HMSelectedRegion @"HMSelectedRegion"
#define HMSelectedSubRegionName @"HMSelectedSubRegionName"
#define HMNotificationCenter [NSNotificationCenter defaultCenter]
#define HMAddObsver(methodName, noteName) [HMNotificationCenter addObserver:self selector:@selector(methodName) name:noteName object:nil];
#define HMRemoveObsver [HMNotificationCenter removeObserver:self];
@interfaceSCUserProfileEntity :NSObject
//通过环信ID去保存昵称和头像url到本地
+ (void)saveUserProfileWithUsername:(NSString*)username forNickName:(NSString*)nickName avatarURLPath:(NSString*)avatarURLPath;
//通过环信ID取本地的昵称
+ (NSString*)getNickNameWithUsername:(NSString*)username;
//通过环信ID取本地的头像url
+ (NSString*)getavatarURLPathWithUsername:(NSString*)username;
//移除本地的头像昵称,清缓存
+ (void)removeUserProfileWithUsername:(NSString*)username;
@implementationSCUserProfileEntity
+ (void)saveUserProfileWithUsername:(NSString*)username forNickName:(NSString*)nickName avatarURLPath:(NSString*)avatarURLPath{
NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];
//设置保存的头像和昵称的key,加上环信ID字段标识
NSString*nickNameKey = [NSStringstringWithFormat:@"username_%@",username];
NSString*avatarURLPathKey = [NSStringstringWithFormat:@"avatarURLPath_%@",username];
[defaultssetObject:nickNameforKey:nickNameKey];
[defaultssetObject:avatarURLPathforKey:avatarURLPathKey];
[defaultssynchronize];
}
+ (NSString*)getNickNameWithUsername:(NSString*)username{
NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];
NSString*nickNameKey = [NSStringstringWithFormat:@"username_%@",username];
return[defaultsobjectForKey:nickNameKey];
}
+ (NSString*)getavatarURLPathWithUsername:(NSString*)username{
NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];
NSString*avatarURLPathKey = [NSStringstringWithFormat:@"avatarURLPath_%@",username];
return[defaultsobjectForKey:avatarURLPathKey];
}
+ (void)removeUserProfileWithUsername:(NSString*)username{
NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];
NSString*nickNameKey = [NSStringstringWithFormat:@"username_%@",username];
NSString*avatarURLPathKey = [NSStringstringWithFormat:@"avatarURLPath_%@",username];
[defaultsremoveObjectForKey:nickNameKey];
[defaultsremoveObjectForKey:avatarURLPathKey];
[defaultssynchronize];
}
1.头像点击方法 messageModel.isSender来判断发送方和接收方
- (void)messageViewController:(EaseMessageViewController*)viewController
didSelectAvatarMessageModel:(id)messageModel
model.avatarURLPath= [SCUserProfileEntitygetavatarURLPathWithUsername:message.from];
用自己写的类去给对应的头像赋值,这里有个问题,如果用户换了头像之后,但是服务器的网址并没有更改,这时是调用缓存来赋值,不能达到实时更新的额效果,需要先清除对应的缓存.
2.设置头像的方法
- (id)messageViewController:(EaseMessageViewController*)viewController
modelForMessage:(EMMessage*)message
使用环信的UI有个主意的地方
chatViewController中[self tableViewDidTriggerHeaderRefresh];要注释掉,父类中已经调用过了.不注释会出现进入聊天的记录出现重复.
获取未读消息时
是需要在获取的页面判断环信的登陆状态的,
其他设备登陆时的回调方法
- (void)didLoginFromOtherDevice
网友评论