Last active
August 29, 2015 14:06
-
-
Save sam-w/c358c90d10b0115bcb3c to your computer and use it in GitHub Desktop.
Method swizzling via category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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