iOS学习ios通信iOS移动开发

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

2015-05-14  本文已影响2556人  Stark_Dylan

记一次解决问题的心得-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 哈哈、

上一篇 下一篇

猜你喜欢

热点阅读