Monday, February 4, 2013

swizzleMethodsForClass

swizzleMethodsForClass.m Select all
#import <objc/runtime.h> // swap a class's instance method selectors, we do this to overload existing methods in category declarations void swizzleMethodsForClass(Class c, SEL origMethodSel, SEL newMethodSel) { NSLog(@"swizzling %@ instance methods: %@ -> %@", NSStringFromClass(c), NSStringFromSelector(origMethodSel), NSStringFromSelector(newMethodSel)); Method origMethod = class_getInstanceMethod(c, origMethodSel); Method newMethod = class_getInstanceMethod(c, newMethodSel); // check if method is inherited from superclass if(class_addMethod(c, origMethodSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, newMethodSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); // exchange un-subclassed method else method_exchangeImplementations(origMethod, newMethod); } @interface UIDevice (SpoofUDID) @end #define UDID_TO_SPOOF @"e0101010d38bde8e6740011211af315301010223" @implementation UIDevice (SpoofUDID) // swizzle this instance method for UIDevice class - (NSString *) spoofUniqueIdentifier { static NSString *spoofUDID = UDID_TO_SPOOF; NSLog(@"spoofing %@ instead of %@", spoofUDID, [[UIDevice currentDevice] spoofUniqueIdentifier]); return spoofUDID; } @end // call this from your app delegate - (void) initUDID { NSString *UDID = [[UIDevice currentDevice] uniqueIdentifier]; NSLog(@"this is my old udid: %@", UDID); swizzleMethodsForClass([UIDevice class], @selector(uniqueIdentifier), @selector(spoofUniqueIdentifier)); NSString *UDID2 = [[UIDevice currentDevice] uniqueIdentifier]; NSLog(@"this is my new udid: %@", UDID2); }



No comments: