美文网首页iOS Developer
环信3.0功能性代码封装(一)

环信3.0功能性代码封装(一)

作者: 见哥哥长高了 | 来源:发表于2017-05-04 20:16 被阅读85次

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

发送文字####

/**
 *  @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功能性代码封装(一)

    本文链接:https://www.haomeiwen.com/subject/vwpdtxtx.html