優化環信聊天信息點擊跳轉其他控制器

注:從我的CSDN博客轉入

這個需求真心把我郁悶了半天,很少見要從聊天信息的文本跳轉到原生的請求界面去

方案1:---有BUG -不建議采取此方式
1.找到EMChatTextBubbleView.m

2.此處是環信的文本點擊事件,再次獲取擴展字段,進行判斷是否匹配,匹配成功發送通知

-(void)bubbleViewPressed:(id)sender  
{  
  
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;  
    CGPoint point = [tap locationInView:self];  
    CFIndex charIndex = [self characterIndexAtPoint:point];  
      
      
    [self highlightLinksWithIndex:NSNotFound];  
    
      
    NSDictionary *extTest = self.model.message.ext;  
    NSLog(@"文本消息%@",extTest);  
    NSString * extString= [NSString stringWithFormat:@"%@",[extTest objectForKey:@"type"]];  
  
    if([extString isEqualToString:@"1"])  
    {  
          
        [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpUserRquestNotification"  
                                                            object:nil];  
          
    }  
    for (NSTextCheckingResult *match in _urlMatches) {  
          
        if ([match resultType] == NSTextCheckingTypeLink) {  
              
            NSRange matchRange = [match range];  
              
            if ([self isIndex:charIndex inRange:matchRange]) {  
                  
                [self routerEventWithName:kRouterEventTextURLTapEventName userInfo:@{KMESSAGEKEY:self.model, @"url":match.URL}];  
                break;  
            }  
        } else if ([match resultType] ==  NSTextCheckingTypeReplacement) {  
              
            NSRange matchRange = [match range];  
              
            if ([self isIndex:charIndex inRange:matchRange]) {  
                  
                [self routerEventWithName:kRouterEventMenuTapEventName userInfo:@{KMESSAGEKEY:self.model, @"text":match.replacementString}];  
                break;  
            }  
        }  
          
    }  
}  

3.前往ChatViewController.m ViewDidLoad里定義通知,監聽方法

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pressbtn:) name:@"jumpUserRquestNotification" object:nil]; 

方案2:

1.消息界面箭頭所指的地方點擊跳轉


2.找到ChatViewController.m
方法定位:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    if (indexPath.row < [self.dataSource count]) {  
        id obj = [self.dataSource objectAtIndex:indexPath.row];  
        if ([obj isKindOfClass:[NSString class]]) {  
            EMChatTimeCell *timeCell = (EMChatTimeCell *)[tableView dequeueReusableCellWithIdentifier:@"MessageCellTime"];  
            if (timeCell == nil) {  
                timeCell = [[EMChatTimeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageCellTime"];  
                timeCell.backgroundColor = [UIColor clearColor];  
                timeCell.selectionStyle = UITableViewCellSelectionStyleNone;  
            }  
              
            timeCell.textLabel.text = (NSString *)obj;  
            return timeCell;  
        }  
        else{  
            
              
            MessageModel *model = (MessageModel *)obj;  
            PopAccount *accuount = [PopAccountTool account];  
            NSString *cellIdentifier = [EMChatViewCell cellIdentifierForMessageModel:model];  
            EMChatViewCell *cell = (EMChatViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
//此處給cell添加點擊方法addTarget:self action:@selector(pressbtn:)  
  
            if (cell == nil) {  
                cell = [[EMChatViewCell alloc] initWithMessageModel:model reuseIdentifier:cellIdentifier addTarget:self action:@selector(pressbtn:)];  
                cell.backgroundColor = [UIColor clearColor];  
                cell.selectionStyle = UITableViewCellSelectionStyleNone;  
            }  
            if (model.isSender) {  
                 
                model.headImageURL = [NSURL URLWithString:accuount.facefile];  
  
            }else{  
                  
                model.headImageURL = [NSURL URLWithString:self.receiverHeaderUrl];  
                  
            }  
            cell.messageModel = model;  
            cell.tag = indexPath.row+1;  
              
              
            return cell;  
        }  
    }  
      
    return nil;  
}  

3.獲取環信提供的擴展字段和我方服務器發出消息的擴展字段進行匹配

-(void)pressbtn:(id)sender{  
    
   
  MessageModel *model = [self.dataSource objectAtIndex:1];  
  NSDictionary *dicExt = model.message.ext;  
  NSString *extString = [NSString stringWithFormat:@"%@",[dicExt objectForKey:@"type"]];  

  if ([extString isEqualToString:@"1"]) {  
      RceiveViewController * rect = [[RceiveViewController alloc]init];  
      UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:rect];  
        
      [self presentViewController:nav animated:YES completion:nil];  
  }  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 點擊查看原文 Web SDK 開發手冊 SDK 概述 網易云信 SDK 為 Web 應用提供一個完善的 IM 系統...
    layjoy閱讀 13,890評論 0 15
  • 由于最近接觸到的項目需要用到聊天功能,關于聊天的SDK跟安卓同事統一,最終選擇了環信。在官方下載好SDK,并研究了...
    十字云閱讀 4,759評論 22 56
  • 今天是大姨的生日,所以一大早就被家里微信群的紅包轟炸醒了,感覺這種醒來的方式簡直太幸福了,咔咔搶紅包。 ...
    巧琳的世界閱讀 115評論 0 1
  • 年少不認真讀書,長大惹來愁緒千絲。 有沒有那么一刻突然很后悔自己在該讀書的年紀卻選擇了偷懶。 正如前人所言“年少不...
    湯小米的小窩閱讀 231評論 0 1
  • 文/亦珺 那個一直亮著燈光的窗子 徹夜徹夜的響著呻吟 蓮無聲的垂首 這世間有億億萬萬朵蓮 卻只有一個觀世音菩...
    亦珺閱讀 334評論 0 7