2

I've implemented a Swizzling category for UIViewControllers, which just NSlogs when they are presented:

#import "UIViewController+Logger.h"
#import <objc/runtime.h>

@implementation UIViewController (Logger)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        SEL originalSelector = @selector(presentViewController:animated:completion:);
        SEL swizzledSelector = @selector(logAndPresentViewController:animated:completion:);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }

    });
}

#pragma mark - Method Swizzling

- (void)logAndPresentViewController:(UIViewController *)viewControllerToPresent
                     animated:(BOOL)flag
                   completion:(void (^)(void))completion {
    NSLog(@"viewControllerToPresent: %@", viewControllerToPresent);

    [self logAndPresentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end

Obviously, this works fine only inside the swizzled application, but I was wondering if I can also "hook" the shared presentViewController so that it is called when a presenting a view outside my application. I was thinking to maybe load the UIViewController dynamically with dlopen and than get a pointer to it's global image symbols. Is it possible? If not - way isn't it?

Disclaimer - I am doing this for debugging on my own development device, and will not upload the app to App Store.

Elist
  • 5,313
  • 3
  • 35
  • 73

0 Answers0