Skip to content

Instantly share code, notes, and snippets.

@sam-w
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sam-w/c358c90d10b0115bcb3c to your computer and use it in GitHub Desktop.
Save sam-w/c358c90d10b0115bcb3c to your computer and use it in GitHub Desktop.
Method swizzling via category
SEL originalSelector = @selector(userInterfaceIdiom);
SEL swizzledSelector = @selector(padUserInterfaceIdiom);
Method originalMethod = class_getInstanceMethod([UIDevice class], originalSelector);
Method swizzledMethod = class_getInstanceMethod([UIDevice class], swizzledSelector);
BOOL didAddMethod =
class_addMethod([UIDevice class],
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod([UIDevice class],
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
}
else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment