Logging the class name of all UIViewControllers in a project

后端 未结 7 749
一生所求
一生所求 2020-12-28 22:51

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

相关标签:
7条回答
  • 2020-12-28 23:14

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

    0 讨论(0)
  • 2020-12-28 23:16

    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);
        }
    
    0 讨论(0)
  • 2020-12-28 23:17

    Here is a solution to print the current view controller class name when it appears, in the console:

    • Create a Symbolic Breakpoint in Xcode
    • for Symbol, add -[UIViewController viewWillAppear:]
    • for Action, add a Debugger Command and the following expression:
    • expr -- (void) printf("
    0 讨论(0)
  • 2020-12-28 23:28

    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.)

    0 讨论(0)
  • 2020-12-28 23:29

    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
    
    0 讨论(0)
  • 2020-12-28 23:35

    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));"

    0 讨论(0)
提交回复
热议问题