美文网首页iOS实战开发iOS开发资料收集区
优化环信聊天信息点击跳转其他控制器

优化环信聊天信息点击跳转其他控制器

作者: 暖游丶 | 来源:发表于2016-06-20 14:55 被阅读550次

    注:从我的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];  
      }  
    

    相关文章

      网友评论

        本文标题:优化环信聊天信息点击跳转其他控制器

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