diff options
Diffstat (limited to 'test/ios/LocationMocker/CSSwizzler.m')
-rw-r--r-- | test/ios/LocationMocker/CSSwizzler.m | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/test/ios/LocationMocker/CSSwizzler.m b/test/ios/LocationMocker/CSSwizzler.m new file mode 100644 index 0000000000..70c5b12bc1 --- /dev/null +++ b/test/ios/LocationMocker/CSSwizzler.m @@ -0,0 +1,44 @@ +// +// Based on gist by Eric Allam +// https://gist.github.com/ericallam/5689235 +// + +#import "CSSwizzler.h" +#import <objc/message.h> +#import <objc/runtime.h> + +@implementation CSSwizzler + ++ (void)swizzleClass:(id)cls + replaceClassMethod:(SEL)origMethodSelector + withMethod:(SEL)replacementMethodSelector; +{ + Method origMethod = nil, altMethod = nil; + origMethod = class_getClassMethod(cls, origMethodSelector); + altMethod = class_getClassMethod(cls, replacementMethodSelector); + method_exchangeImplementations(origMethod, altMethod); +} + ++ (void)swizzleClass:(id)cls + replaceMethod:(SEL)origMethodSelector + withMethod:(SEL)replacementMethodSelector; +{ + Method origMethod = nil, altMethod = nil; + origMethod = class_getInstanceMethod(cls, origMethodSelector); + altMethod = class_getInstanceMethod(cls, replacementMethodSelector); + method_exchangeImplementations(origMethod, altMethod); +} + ++ (void)swizzleClassOfInstance:(id)inst + replaceMethod:(SEL)origMethodSelector + withMethod:(SEL)replacementMethodSelector; +{ + const char *str = [[[inst class] description] UTF8String]; + Class cls = objc_getClass(str); + Method origMethod = nil, altMethod = nil; + origMethod = class_getInstanceMethod(cls, origMethodSelector); + altMethod = class_getInstanceMethod(cls, replacementMethodSelector); + method_exchangeImplementations(origMethod, altMethod); +} + +@end |