美文网首页iOS学习ios通信iOS移动开发
记一次解决问题的心得-iOS, 简易Hack融云SDK

记一次解决问题的心得-iOS, 简易Hack融云SDK

作者: Stark_Dylan | 来源:发表于2015-05-14 19:39 被阅读2556次

    记一次解决问题的心得-iOS, 简易Hack融云SDK

    今天在看融云的群, 偶然发现有人有一种需求:

    屏幕快照 2015-05-14 下午7.29.48.png

    另一个问题,有没有办法让进入聊天页面的时候,在输入框里预先输好一段文字。光标定位在后面,可以继续输

    为了帮这位用户解决问题,首先去看了一下融云提供的IMKit的聊天界面的父类RCChatViewController

    我是继承了一下

    屏幕快照 2015-05-14 下午7.30.39.png

    看到头文件属性的位置

    细心的看到了 @property (strong, nonatomic) RCChatSessionInputBarView *msgInputBar; 有这样一个属性, 认定这就是我们的目标, 因为输入框肯定是放在这里的。

    但是看到融云的SDK也是封掉了这个类的开发, 只使用@class来引用,

    屏幕快照 2015-05-14 下午7.32.48.png

    所以不能直接的拿到属性以及方法。

    所以想到了运行时, 首先导入运行时的头文件

    #import <objc/runtime.h>

    然后呢 先去获得这个目标对象的属性列表, 通过名称简单的判断一下我们的输入框是什么。

        NSString *className = @"RCChatSessionInputBarView";
        const char * cClassName = [className UTF8String];
        id classM = objc_getClass(cClassName);
        unsigned int outCount, i;
        
        objc_property_t * properties = class_copyPropertyList(classM, &outCount);
        
        for (i = 0; i < outCount; i++) {
            objc_property_t property = properties[i];
            NSString * attributeName = [NSString stringWithUTF8String:property_getName(property)];
            NSLog(@"%@", attributeName);
        }
    
    

    这样 打印除了所有的属性列表。

    2015-05-14 19:15:14.633 SuperMan[1573:359106] audioBtn
    
    2015-05-14 19:15:14.633 SuperMan[1573:359106] emojiBtn
    
    2015-05-14 19:15:14.633 SuperMan[1573:359106] additionalBtn
    
    2015-05-14 19:15:14.634 SuperMan[1573:359106] msgColumnTextView
    
    2015-05-14 19:15:14.638 SuperMan[1573:359106] pressTalkButton
    
    2015-05-14 19:15:14.638 SuperMan[1573:359106] parent
    
    2015-05-14 19:15:14.638 SuperMan[1573:359106] currentPosY
    
    2015-05-14 19:15:14.639 SuperMan[1573:359106] hash
    
    2015-05-14 19:15:14.639 SuperMan[1573:359106] superclass
    
    2015-05-14 19:15:14.640 SuperMan[1573:359106] description
    
    2015-05-14 19:15:14.640 SuperMan[1573:359106] debugDescription
    

    我们可以看到结果中有 msgColumnTextView

    明显就是这个了、 所以拿到这个对象 就OK了、

    最终解决办法

        // Hack Rong Cloud
        NSString *className = @"RCChatSessionInputBarView";
        const char * cClassName = [className UTF8String];
        id classM = objc_getClass(cClassName);
        unsigned int outCount, i;
        
        objc_property_t * properties = class_copyPropertyList(classM, &outCount);
        
        for (i = 0; i < outCount; i++) {
            objc_property_t property = properties[i];
            NSString * attributeName = [NSString stringWithUTF8String:property_getName(property)];
            NSLog(@"%@", attributeName);
            if ([attributeName isEqualToString:@"msgColumnTextView"]) {
                
                id currentObject = self.msgInputBar;
                [currentObject setValue:@"text" forKeyPath:@"msgColumnTextView.text"];
                UITextView * textView = [currentObject valueForKey:@"msgColumnTextView"];
                [textView becomeFirstResponder];
            }
        }
    

    这样 就完成了我们的目的、、、哈哈、

    Screen Shot 2015-05-14 at 下午7.37.28.png

    遇到问题不要逃避、换个角度去思考,首先我们认识到了思维的重要性,然后我们要学会怎么去合理的使用运行时来帮我们完成事情。 这只是一个最简单的例子。 运行时庞大到可以操作任何你想要的存在的对象, 加油吧。 欢迎一起讨论。 喜欢请Mark 哈哈、

    相关文章

      网友评论

      • 乱了夏末丶蓝了海:大神,你有没有做过 群组改名或是T任,在群组显示提示(融云demo中的那种效果)呢?
        Stark_Dylan:@乱了夏末丶蓝了海 这个需要服务端配合一下最好做了。
      • SMFly:我看融云分享未知的时候偏移了,这个情况,你怎么解决的。不会也是hack。。 :smile:
        Stark_Dylan:@WildDylan 子类中, - (void)willDisplayMessageCell:(RCMessageBaseCell *)cell
        atIndexPath:(NSIndexPath *)indexPath , 然后[cell isKindOfClass:[RCMessageCell class]] 判断是什么类型的Cell, 然后运行时查看一下拥有的属性名(reveal查看子视图)。

        例如: UIImageView * imageView = [currentCell valueForKey:@"portraitImageView"];
        currentCell.messageContentView.top = imageView.top + 1;
        Stark_Dylan:@SMFly 这个可以在它提供的代理方法中修改
        SMFly:@SMFly 位置
      • SMFly:略diao! :+1:
        Stark_Dylan:@SMFly 瞎玩...

      本文标题:记一次解决问题的心得-iOS, 简易Hack融云SDK

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