iOS环信3.0发送文字,语音,位置,图片封装代码

因为项目要求,花了快一个月的时间都在做环信的即时聊天,中间也是查查找找,看看别人的代码,看看环信的文档,所以现在打算整合一份给大家参考。

项目中只做了文字,语音,位置,图片发送。看这篇文章的时候默认读者已经阅读了解环信3.0文档。

以下代码都是封装好的,可以直接调用

发送文字

/**
 *  @param txtStr 消息文字
 *  @param chatBossTel 接收方
 */
#pragma mark 环信-发送文字动作
-(void)sendMessage:(NSString *)txtStr
{
  //加入子线程,不会造成卡顿
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //生成消息
        EMTextMessageBody *body=[[EMTextMessageBody alloc]initWithText:txtStr];
        //消息发送方
        NSString *from=[[EMClient sharedClient]currentUsername];
        
        //消息的接收方
        EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
        //消息类型是单聊
        message.chatType=EMChatTypeChat;
        
        //发送消息
        [[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
        } completion:^(EMMessage *message, EMError *error) {
            CLog(@"发送的消息错误原因%@",error);
            if (!error)
            {
                //把消息加入数组
                [self.messArr addObject:message];
                //计算高度
                [self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
                //滑动到最后一行
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self tableViewScrollToBottom];
                });
            }
        }];
    });
}

发送语音

#pragma mark环信-发送语音
/**
 *  @param localPath   语音地址
 *  @param duration    语音秒数
 */
-(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];
        //语音的秒数
        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];
        //设为单聊
        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];
            });
        }
    });
}

发送位置

#pragma mark 环信-发送位置
/**
 *  @param latitude  纬度
 *  @param longitude 经度
 *  @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];
         //设为单聊
         message.chatType=EMChatTypeChat;
         //发送语音
         [[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
         } completion:^(EMMessage *message, EMError *error) {
             CLog(@"发送位置的错误%@",error);
             if (!error)
             {
                 //加入tab
                 [self.messArr addObject:message];
                //计算高度
                 [self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
                 dispatch_async(dispatch_get_main_queue(), ^{
                     [self tableViewScrollToBottom];
                 });
             }
         }];
    });
}

发送图片

#pragma mark 相册代理 发送图片
//从相册中获取图片进行发送,正在发送时带了一个菊花
-(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];//显示菊花
    //发送图片
    [[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
        if (progress==100)
        {
            [hud hide:YES];//隐藏菊花
        }
    } completion:^(EMMessage *message, EMError *error) {
        CLog(@"发送图片Error%@",error);
        if (!error)
        {   //存入数组
            [self.messArr addObject:message];
            [self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
            [self tableViewScrollToBottom];
        }
    }];
    [picker dismissViewControllerAnimated:YES completion:nil];//模态视图
}
以上就是发送文字,语音,位置,图片的封装,可以直接调用。一些常用类下一次会给你们列出来。
使用到的方法:

环信3.0项目中用到的一些封装的类方法

你可能感兴趣的:(iOS环信3.0发送文字,语音,位置,图片封装代码)