Logging the class name of all UIViewControllers in a project

We have received a HUGE project from outsourcing that we are trying to “repair”. There are hundreds of view controllers within the project. Our goal is to easily determine which class we are currently looking at on the device.

Our solution (which didn’t work, hence the SO question) follows.

  • How to curve the top of a UIView Controller in Swift
  • Navigation bar becomes transparent when set to translucent
  • how do i change the default ViewController in xcode
  • How do I go from SKScene to UIViewController by code?
  • Handling Rotation in iOS6
  • Having Problems With UISearchBar in UITableViewController
  • Override the viewDidAppear method of UIViewController via a category with this:

    -(void)viewDidAppear:(BOOL)animated
    {
        NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
        [self viewDidAppear:animated];
        //Also tried this:
        //[super viewDidAppear:animated];
    }
    

    This category would be put in the .pch of the project.

    This would require no extra code to be put in the hundreds of View Controllers and be easily turned on and off. It didn’t work because, as we’ve learned now, <meme>one does not simply override an existing method via category</meme>.

    What are we missing?!?

    6 Solutions Collect From Internet About “Logging the class name of all UIViewControllers in a project”

    The answer is to swizzle the methods! Here is what we came up with:

    #import "UIViewController+Logging.h"
    #import <objc/runtime.h>
    
    @implementation UIViewController (Logging)
    
    -(void)swizzled_viewDidAppear:(BOOL)animated
    {
        NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
        [self swizzled_viewDidAppear:animated];
    }
    
    + (void)load
    {
        Method original, swizzled;
    
        original = class_getInstanceMethod(self, @selector(viewDidAppear:));
        swizzled = class_getInstanceMethod(self, @selector(swizzled_viewDidAppear:));
    
        method_exchangeImplementations(original, swizzled);
    
    }
    @end
    

    Here is solution for this

    In your .pch file include this

    #define UIViewController MyViewController
    #import "MyViewController.h"
    

    Create your new UIViewController sub class as

    .h file

    #import <UIKit/UIKit.h>
    
    #ifdef UIViewController
    #undef UIViewController
    #endif
    @interface MyViewController : UIViewController
    
    @end
    #ifndef UIViewController
    #define UIViewController MyViewController
    #endif
    

    And .m file

    #import "MyViewController.h"
    
    @implementation MyViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
    }
    
    @end
    

    Do the view controllers share a common base class? if so you could just put it there in the base class’ implementation of [viewDidAppear:]. If they do not share a common base, then perhaps that would be a worthwhile task as it could be useful anyways going forwards (common analytics code, etc.)

    You can do a application wide find and replace from Xcode, but it won’t necessarily find every case (but neither would the approaches that you tried). You could look for “[super viewDidLoad];” and replace with “[super viewDidLoad]; NSLog(@”Current View Class: %@”, NSStringFromClass(self.class));”

    Does the app use Navigation controllers to display the View Controllers? If so, you can use the NavigationController’s methods to report the current controller:

        - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
        {
            [self reportNewController:viewController];
        }
    
        - (void) reportNewController:(UIViewController *)viewController
        {
            NSString *name = viewController.title;
                NSLog(@"Name is %@",name);
        }
    

    You can use method swizzling. Here is a nice guide:
    http://nshipster.com/method-swizzling/