0

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
Neal.Marlin
  • 494
  • 5
  • 17
  • Please include only _relevant_ code in your question. It's disrespectful to dump whole classes and make people read through code that has nothing to do with what you're asking (e.g empty. `viewDidLoad` `didReceiveMemoryWarning` methods), Please update your question. – Ashley Mills Aug 19 '18 at 17:04
  • 1
    I'm not sure if this will solve it, but I think `Class cls = object_getClass(self);` should be `Class class = [self class];`. Does that fix it? – TylerP Aug 19 '18 at 17:07
  • 1
    @TylerTheCompiler Yeah, Finally I found the problem. cause this `+load` is a class method, `self`or `[self class]` presents the UIViewController class and `object_getClass(self)` just gets the meta class of UIViewController. Thank you! – Neal.Marlin Aug 19 '18 at 18:01
  • @AshleyMills OK, I’ll do it. – Neal.Marlin Aug 26 '18 at 11:56

1 Answers1

2

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.

TylerP
  • 9,600
  • 4
  • 39
  • 43