因為項目要求岛啸,花了快一個月的時間都在做環(huán)信的即時聊天钓觉,中間也是查查找找,看看別人的代碼坚踩,看看環(huán)信的文檔荡灾,所以現(xiàn)在打算整合一份給大家參考。
項目中只做了文字瞬铸,語音批幌,位置,圖片發(fā)送嗓节∮担看這篇文章的時候默認(rèn)讀者已經(jīng)閱讀了解環(huán)信3.0文檔。
以下代碼都是封裝好的拦宣,可以直接調(diào)用
發(fā)送文字
/**
* @param txtStr 消息文字
* @param chatBossTel 接收方
*/
#pragma mark 環(huán)信-發(fā)送文字動作
-(void)sendMessage:(NSString *)txtStr
{
//加入子線程截粗,不會造成卡頓
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//生成消息
EMTextMessageBody *body=[[EMTextMessageBody alloc]initWithText:txtStr];
//消息發(fā)送方
NSString *from=[[EMClient sharedClient]currentUsername];
//消息的接收方
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
//消息類型是單聊
message.chatType=EMChatTypeChat;
//發(fā)送消息
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
CLog(@"發(fā)送的消息錯誤原因%@",error);
if (!error)
{
//把消息加入數(shù)組
[self.messArr addObject:message];
//計算高度
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
//滑動到最后一行
dispatch_async(dispatch_get_main_queue(), ^{
[self tableViewScrollToBottom];
});
}
}];
});
}
發(fā)送語音
#pragma mark環(huán)信-發(fā)送語音
/**
* @param localPath 語音地址
* @param duration 語音秒數(shù)
*/
-(void)sendVoiceWithLocalPath:(NSString*)localPath
andDisplayName:(NSString*)displayName
andDuration:(int)duration
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//生成語音
EMVoiceMessageBody *body=[[EMVoiceMessageBody alloc]initWithLocalPath:localPath displayName:displayName];
//語音的秒數(shù)
body.duration=self.recordV.duration;
NSString *from=[[EMClient sharedClient]currentUsername];
//生成Mes
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
//設(shè)為單聊
message.chatType=EMChatTypeChat;
if (duration>1)//大于1秒
{
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
//加入tab
[self.messArr addObject:message];
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
dispatch_async(dispatch_get_main_queue(), ^{
[self tableViewScrollToBottom];
});
}];
}
else
{
//停止錄音
[self.recordV stopRecordVocie];
//刪除錄音
[self.recordV deleteVoice];
dispatch_async(dispatch_get_main_queue(), ^{
//錄音時間太短請重新
[WarnWindow HUD:self.view andWarnText:@"錄音時間太短請重新" andXoffset:0 andYoffset:0];
});
}
});
}
發(fā)送位置
#pragma mark 環(huán)信-發(fā)送位置
/**
* @param latitude 緯度
* @param longitude 經(jīng)度
* @param location 位置
*/
-(void)sendLocationWithLatitude:(CGFloat)latitude
andLongitude:(CGFloat)longitude
andLocation:(NSString*)location
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//生成位置
EMLocationMessageBody *body=[[EMLocationMessageBody alloc]initWithLatitude:latitude longitude:longitude address:location];
NSString *from=[[EMClient sharedClient]currentUsername];
//生成Mes
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
//設(shè)為單聊
message.chatType=EMChatTypeChat;
//發(fā)送語音
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
CLog(@"發(fā)送位置的錯誤%@",error);
if (!error)
{
//加入tab
[self.messArr addObject:message];
//計算高度
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
dispatch_async(dispatch_get_main_queue(), ^{
[self tableViewScrollToBottom];
});
}
}];
});
}
發(fā)送圖片
#pragma mark 相冊代理 發(fā)送圖片
//從相冊中獲取圖片進(jìn)行發(fā)送,正在發(fā)送時帶了一個菊花
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//從字典中獲取原始對象
UIImage *imgV=info[UIImagePickerControllerOriginalImage];
NSData *data=UIImageJPEGRepresentation(imgV, 0.5);
//生成圖片的data
EMImageMessageBody *body=[[EMImageMessageBody alloc]initWithData:data displayName:@"image.png"];
NSString *from=[[EMClient sharedClient] currentUsername];
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
message.chatType=EMChatTypeChat;
[self creatHud];//顯示菊花
//發(fā)送圖片
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
if (progress==100)
{
[hud hide:YES];//隱藏菊花
}
} completion:^(EMMessage *message, EMError *error) {
CLog(@"發(fā)送圖片Error%@",error);
if (!error)
{ //存入數(shù)組
[self.messArr addObject:message];
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
[self tableViewScrollToBottom];
}
}];
[picker dismissViewControllerAnimated:YES completion:nil];//模態(tài)視圖
}