Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 43 lines (37 sloc) 1.284 kb
fe87b5e @mudphone Updated keynote and added notification center implementation for swiz…
mudphone authored
1 /*
2 - NSNotificationCenter Category Example:
3 - This may not run any more.
4 */
5 #import <objc/runtime.h>
6 #import <objc/message.h>
7
8 @interface NSNotificationCenter (RNHijack)
9 + (void)hijack;
10 @end
11
12 @implementation NSNotificationCenter (RNHijack)
13
14 + (void)hijackSelector:(SEL)originalSelector withSelector:(SEL)newSelector
15 {
16 Class class = [NSNotificationCenter class];
17 Method originalMethod = class_getInstanceMethod(class, originalSelector);
18 Method overrideMethod = class_getInstanceMethod(class, newSelector);
19 // method_exchangeImplementations(originalMethod, overrideMethod); // Can't do this!
20 if (class_addMethod(class, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod)))
21 {
22 class_replaceMethod(class, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
23 }
24 else
25 {
26 method_exchangeImplementations(origMethod, overrideMethod);
27 }
28 }
29
30 + (void)hijack
31 {
32 [self hijackSelector:@selector(removeObserver:)
33 withSelector:@selector(RNHijack_removeObserver:)];
34 }
35
36 - (void)RNHijack_removeObserver:(id)notificationObserver
37 {
38 NSLog(@"Removing observer: %@", notificationObserver);
39 // Pseudo-recursive confusingness:
40 [self RNHijack_removeObserver:notificationObserver];
41 }
42
43 @end
Something went wrong with that request. Please try again.