iOS 环信对话 点击 Url 超链接 跳转

由于来的公司的项目中有集成环信,发现个问题环信对话里有链接点击不能打开,测试说人家安卓可以你们怎么不行。
网上方法有两个,但我都没集成成功。
现在我主要是借助http://www.jianshu.com/p/7909656c96f5 这位大神的方法来完成的。

前三步可以按他的来操作,他的三步主要实现了1.给超链接加蓝色和下划线 2.在我实现了点击文本响应后,识别出文本的超链接,添加的数组中,类型是NSURL,并不是NSSTRING,这点要注意。

为什么我还要再写一篇这样的文字呢,因为我按照他的第三步重写发现我的响应不了点击,原来环信中没有判断text 这个类型,其它图片 表情之类的都可以响应。所以我们要自己添加上去就可以了。

现在来到我这边处理:
1.在EaseMessageViewController.h 写一个代理方法

- (void)touchTextViewMessageModel:(id<IMessageModel>)messageModel;

2.EaseMessageViewController.m 中的

- (void)messageCellSelected:(id<IMessageModel>)model{
}

加多一个text类型的判断去触发代理。

片断代码

……………….
   case EMMessageBodyTypeFile:
        {
            _scrollToBottomWhenAppear = NO;
            [self showHint:@"Custom implementation!"];
        }
            break;
        case EMMessageBodyTypeText:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(touchTextViewMessageModel:)]) {
                [_delegate touchTextViewMessageModel:model];
            }
        }
            break;
        default:
            break;
………………………………

3.去chatViewController.m,重写代理方法
重写这两个任意一个都行

- (BOOL)messageViewController:(EaseMessageViewController *)viewController didSelectMessageModel:(id<IMessageModel>)messageModel{
}

我就重写我们刚才那个代理 结合了船长的方法

- (void)touchTextViewMessageModel:(id<IMessageModel>)messageModel{
    NSLog(@“拿到文本%@",messageModel.text);
    NSString*str=messageModel.text;
    NSDataDetector*detector=[[NSDataDetector alloc]initWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray*array=[detector matchesInString:str options:0 range:NSMakeRange(0, str.length)];
//判断有没有链接
    if([array count]>0)
    {

      ChatwebViewController *vc1 =  [[ChatwebViewController alloc]init];

        if ([array count]>1) {网址多于1 个时让用用户选择跳哪个链接
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择要打开的链接" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                NSLog(@"点击取消");
                
            }]];

            for (int i =0; i<array.count; i++) {
                NSURL *url =[array[i] URL];
                NSString *str = [NSString stringWithFormat:@"%@",url];
                [alertController addAction:[UIAlertAction actionWithTitle:str style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    vc1.url = url;
                    [SVProgressHUD showWithStatus:@"请稍后..."];
                    [self.navigationController pushViewController:vc1 animated:YES];
                    
                }]];
            }

            [self presentViewController:alertController animated:YES completion:nil];
        }else{//一个链接直接打开
            
            vc1.url = [array[0] URL];
            [SVProgressHUD showWithStatus:@"请稍后..."];
            [self.navigationController pushViewController:vc1 animated:YES];
        }
    }

看我的效果 ,只能这样处理多个链接了,有更好的方法请你告诉我。

IMG_4885.PNG
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容