Skip to content

Instantly share code, notes, and snippets.

@swillits
Last active December 15, 2015 10:39
Show Gist options
  • Save swillits/5247673 to your computer and use it in GitHub Desktop.
Save swillits/5247673 to your computer and use it in GitHub Desktop.
void MethodSwizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if (class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
MethodSwizzle(openglcontext, @selector(flushBuffer), @selector(my_flushBuffer));
- (void)my_flushBuffer;
{
[self my_flushBuffer]; // calls the *original* flushBuffer method (yes it looks weird)
<do whatever you want>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment