-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswizzle.m
68 lines (55 loc) · 2.94 KB
/
swizzle.m
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#import <objc/runtime.h>
#import <objc/message.h>
#import <UIKit/UIKit.h>
#import "swizzle.h"
static void SwizzleInstanceMethod(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);
}
static void SwizzleInstanceMethodFromClass(Class c, SEL orig, Class c2, SEL new)
{
//Add the new method from a c2 to c1
Method newMethod = class_getInstanceMethod(c2, new);
class_addMethod(c, new, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
// Then just swizzle
SwizzleInstanceMethod(c, orig, new);
}
static void SwizzleNameInstanceMethodFromClass(NSString *cn, SEL orig, NSString *cn2, SEL new)
{
Class c = NSClassFromString(cn);
Class c2 = NSClassFromString(cn2);
SwizzleInstanceMethodFromClass(c, orig, c2, new);
}
static void SwizzleClassMethod(Class c, SEL orig, SEL new)
{
Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c, new);
Class cMetaClass = objc_getMetaClass(class_getName(c));
if(class_addMethod(cMetaClass, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(cMetaClass, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
static void SwizzleClassMethodFromClass(Class c, SEL orig, Class c2, SEL new)
{
Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c2, new);
Class cMetaClass = objc_getMetaClass(class_getName(c));
if(class_addMethod(cMetaClass, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(cMetaClass, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
//Method to exchange methods in Runtime
void doSwizzle(){
SwizzleNameInstanceMethodFromClass(@"ASIHTTPRequest",@selector(main),@"KIFNetworkDisabler",@selector(newASImain));
SwizzleNameInstanceMethodFromClass(@"AFHTTPRequestOperation",@selector(operationDidStart),@"KIFNetworkDisabler",@selector(newAFoperationDidStart));
SwizzleNameInstanceMethodFromClass(@"KIFTestStep",@selector(executeAndReturnError:),@"KIFTestStep",@selector(suiteExecuteAndReturnError:));
SwizzleNameInstanceMethodFromClass(@"KIFTestScenario",@selector(setStepsToSetUp:),@"KIFTestScenario",@selector(suiteSetStepsToSetUp:));
SwizzleNameInstanceMethodFromClass(@"KIFTestScenario",@selector(setStepsToTearDown:),@"KIFTestScenario",@selector(suiteSetStepsToTearDown:));
}