#import <objc/runtime.h> @interface NSArray(hook) -(id)myLastObject; @end @implementation NSArray(hook) -(id)myLastObject { NSLog(@"inject success!!!"); return [self myLastObject]; } @end void HookMethod(Class aClass, SEL oldSEL, SEL newSel) { Method oldMethod = class_getInstanceMethod(aClass, oldSEL); assert(oldMethod); Method newMethod = class_getInstanceMethod(aClass, newSel); assert(newMethod); method_exchangeImplementations(oldMethod, newMethod); } int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Hello, P.Y.G!"); HookMethod([NSArray class], @selector(lastObject), @selector(myLastObject)); NSArray *array = @[@"piaoyun", @"nisy", @"Q", @"GG"]; NSString *string = [array lastObject]; NSLog(@"RET : %@", string); } return 0; }
发表评论