IOS swizzling API method for private infrastructure

I know that there are countless resources on the swizzling method. However, is it possible to throw a method out of a private API? The problem is that there are no header files. I would like to swizzle a method from a private class in PrivateFramework, such as the (random example) Message.framework methods

This is for personal testing, I understand that Apple will be rejected by oblivion.

+7
ios objective-c reverse-engineering iphone-privateapi
source share
2 answers

You can use NSClassFromString to get the Class and use the runtime library to execute the swizzling method. No header files. You just need to know the class name and method signature.

sel_getUid can be used when @selector(somePrivateMethod) gives your error about somePrivateMethod invalid selector (because the header is not available)

Code taken from my Xcode plugin

 SEL sel = sel_getUid("codeDiagnosticsAtLocation:withCurrentFileContentDictionary:forIndex:"); Class IDEIndexClangQueryProviderClass = NSClassFromString(@"IDEIndexClangQueryProvider"); Method method = class_getInstanceMethod(IDEIndexClangQueryProviderClass, sel); IMP originalImp = method_getImplementation(method); IMP imp = imp_implementationWithBlock(^id(id me, id loc, id dict, IDEIndex *idx) { id ret = ((id (*)(id,SEL,id,id,id))originalImp)(me, sel, loc, dict, idx); // do work return ret; }); method_setImplementation(method, imp); 
+9
source share

Create a category in the class and add an declaration for the method you want to call. Then you can simply instantiate the class and call the method.

It also works for individually testing private methods in your code.

0
source share

All Articles