Nick
Nick

Reputation: 11

objective-c how to get private Method lists

as the title,I want to know the private method list of an objective-c class. such as "_resetViewController" for UIviewcontroller.

Upvotes: 0

Views: 578

Answers (3)

siburb
siburb

Reputation: 5017

There are a few regularly updated repositories containing the headers - here's a good one (for academic purposes only of course 😉): https://github.com/nst/iOS-Runtime-Headers

As @uliwitness mentioned, it is dangerous using private APIs in production code.

Upvotes: 0

Nick
Nick

Reputation: 11

I try to use runtime log the method list as follow:

- (void)testPrivateMethod {
    Class clz = [self class];
    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList(clz, &methodCount);
    printf("Found %d methods on '%s'\n", methodCount, 
    class_getName(clz));
    for (unsigned int i = 0; i < methodCount; i++) {
        Method method = methods[i];
        printf("\t'%s' has method named '%s' of encoding '%s'\n",
               class_getName(clz),
               sel_getName(method_getName(method)),
               method_getTypeEncoding(method));
    }
    free(methods);
}

but,I just got three method as follow:

Found 3 methods on 'HGMThirdViewController'
    'HGMThirdViewController' has method named 'testPrivateMethod' of encoding 'v16@0:8'
    'HGMThirdViewController' has method named 'viewDidLoad' of encoding 'v16@0:8'
    'HGMThirdViewController' has method named 'didReceiveMemoryWarning' of encoding 'v16@0:8'

Upvotes: 1

uliwitness
uliwitness

Reputation: 8843

Look at objc/runtime.h, in particular the class_copyMethodList() function and the method_* functions that go with it.

Or if you're looking for a developer tool and not trying to write one, search for class-dump.

Note: Do not ship applications that call private methods, stick to the ones that are documented. Many of these methods may just be a convenience for the developer implementing the public methods, and they will change them when they make additions to the public code. They may remove the methods entirely, or just change the type of a parameter, and if your app calls them, it will suddenly crash, or corrupt memory and make something else crash 5 hours later and you'll have no idea why it is crashing.

Upvotes: 2

Related Questions