Last active
July 2, 2017 10:55
-
-
Save mavris/1dcbe34984d5cc9c7e0ec1ac244f5b3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSUserDefaults+MonitoringWrites.h" | |
#import <objc/runtime.h> | |
@implementation NSUserDefaults (MonitoringWrites) | |
+ (void)load { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
Class class = [self class]; | |
SEL defaultSelector = @selector(setObject:forKey:); | |
SEL swizzledSelector = @selector(swizzled_setObject:forKey:); | |
Method defaultMethod = class_getInstanceMethod(class, defaultSelector); | |
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | |
BOOL isMethodExists = !class_addMethod(class, defaultSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); | |
if (isMethodExists) { | |
method_exchangeImplementations(defaultMethod, swizzledMethod); | |
} | |
else { | |
class_replaceMethod(class, swizzledSelector, method_getImplementation(defaultMethod), method_getTypeEncoding(defaultMethod)); | |
} | |
}); | |
} | |
#pragma mark - Method Swizzling | |
- (void)swizzled_setObject:(id)value forKey:(NSString *)defaultName { | |
[self swizzled_setObject:value forKey:defaultName]; | |
NSLog(@"Set Object %@ for key %@",value,defaultName); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment