summaryrefslogtreecommitdiff
path: root/test/ios/LocationMocker/CSSwizzler.m
blob: 70c5b12bc139d14f67222689cb73445b938dcffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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