IOSiOS开发专题我的待读文章

iOS 利用runTime进行“私有方法”替换

2017-11-28  本文已影响132人  劉光軍_MVP

写在前面

首先声明:题目中所说的“私有方法”只是我们感官上的感觉,OC 中没有绝对的私有变量和私有方法。

关于私有变量和“私有方法”

但是要注意的是:OC是动态性语言,他的对象类型和真正要调用的方法是在运行时才确定的,所以这就决定了在oc中没有绝对的私有变量和私有方法的,通过runtime我们可以动态的去对类中所有的变量和方法进行操作,关于runtime的一些东西在
《iOS-RunTime介绍及使用 http://www.jianshu.com/p/5408dcab8f02》中介绍。比如通过class_copyIvarListclass_copyMethodList这两个方法。

正题

我们在项目中可能会遇到这样的情况,你引入了一个库,库里面的.m文件中的方法你不能修改,但是你又必须要用到,就像我们遇到的场景,我需要跳到库里的mainViewController中,在mainViewController有个navigationController的pop方法,在这个mainViewController中定义的pop方法是popToRootViewControllerAnimated但是我需要的是pop到指定viewController中,但是我们又不能修改mainViewController这个文件中的方法,该怎么办呢?这里说一个利用runtime解决的方法:

替换“私有方法”

在我们当前的VC中,我们生命一个方法changeLiveMethod用来执行替换方法操作。
代码如下:

- (void)popToLiveListVC1
{
    UIViewController *target = nil;
    for (UIViewController * controller in self.navigationController.viewControllers) { //遍历
        if ([controller isKindOfClass:[LiveListViewController class]]) { //这里判断是否为你想要跳转的页面
            target = controller;
        }
    }
    if (target)
    {
        [self.navigationController popToViewController:target animated:YES]; //跳转
    }
}
//当前pop方法1
    SEL thisSelector = @selector(popToLiveListVC1);
    Method thisMethod = class_getInstanceMethod([self class], thisSelector);
    
   //获取mainViewCOntroller 中的gotoViewController方法
//    unsigned int count = 0;
//    Method *membFuns = class_copyMethodList([MainViewController class], &count);
    NSString *mainPopMethodStr = @"gotoViewController";
//    for (int i =0 ; i < count; i++) {
//        SEL name = method_getName(membFuns[i]);
//        NSString *methodName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
//        NSLog(@"m method:%@",methodName);
//        if ([methodName isEqualToString:@"gotoViewController"]) {
//            mainPopMethodStr = methodName;
//        }
//    }
    SEL mainPopSelector = NSSelectorFromString(mainPopMethodStr);
    Method mainPopMethod = class_getInstanceMethod([MainViewController class], mainPopSelector);
    method_exchangeImplementations(thisMethod, mainPopMethod);

代码分析: 先取到当前我们需要的新方法 Method,然后获取库里面MainViewController私有方法,我们用Method mainPopMethod = class_getInstanceMethod([MainViewController class], @selector(gotoViewController));这个方法编译器会报个警告,所以我们用SEL的这个方式获取这个mainPopMethod ,然后进行method_exchangeImplementations方法的交换,这个方法就是改变类的分发表,使它在解析消息的时候,将一个选择器selector对应到另一个替换的实现,并且将该选择器对应的原始实现关联到新的选择器中。

对SEL Method IMP的一些说明
上一篇下一篇

猜你喜欢

热点阅读