forked from mudphone/ObjcPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSNotificationCenter+RNHijack.m
43 lines (37 loc) · 1.25 KB
/
NSNotificationCenter+RNHijack.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
/*
- NSNotificationCenter Category Example:
- This may not run any more.
*/
#import <objc/runtime.h>
#import <objc/message.h>
@interface NSNotificationCenter (RNHijack)
+ (void)hijack;
@end
@implementation NSNotificationCenter (RNHijack)
+ (void)hijackSelector:(SEL)originalSelector withSelector:(SEL)newSelector
{
Class class = [NSNotificationCenter class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method overrideMethod = class_getInstanceMethod(class, newSelector);
// method_exchangeImplementations(originalMethod, overrideMethod); // Can't do this!
if (class_addMethod(class, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod)))
{
class_replaceMethod(class, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}
else
{
method_exchangeImplementations(origMethod, overrideMethod);
}
}
+ (void)hijack
{
[self hijackSelector:@selector(removeObserver:)
withSelector:@selector(RNHijack_removeObserver:)];
}
- (void)RNHijack_removeObserver:(id)notificationObserver
{
NSLog(@"Removing observer: %@", notificationObserver);
// Pseudo-recursive confusingness:
[self RNHijack_removeObserver:notificationObserver];
}
@end