iosobjective-cmethod-swizzling

why this UIViewController method swizzling doesn't work?


I'm trying out a code example of The NSHipster Fake Book to swizzle the viewWillAppear: method of UIViewController. But It seems that it doesn't work expectedly since the xxx_viewWillAppear: method has never been invoked. I just cannot find out why. Please help me, thanks.

#import "ViewController.h"
#import <objc/runtime.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

@end

@implementation UIViewController (Tracking)
+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls = object_getClass(self);
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(xxx_viewWillAppear:);
        Method originalMethod = class_getInstanceMethod(cls, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
        BOOL addMethod = class_addMethod(cls, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (addMethod) {
            class_replaceMethod(cls, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }
        else{
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

-(void)xxx_viewWillAppear:(BOOL)animated
{
    [self xxx_viewWillAppear:animated];
    NSLog(@"viewWillAppear: %@",self);
}
@end

Solution

  • Since load is a class method, the self in Class cls = object_getClass(self); refers to the meta class of UIViewController, not the actual class you want (UIViewController).

    If you change it to Class cls = [self class];, then cls will be the UIViewController class itself and it should work.