Runtime 实现hook方法

@interface TestObject : NSObject

- (void)testMethod:(NSString *)text;

@end


@implementation TestObject

- (void)testMethod:(NSString *)text {

NSLog(@"testMethod : %@", text);

}

@end

// runtime

IMP function = imp_implementationWithBlock(^(id self, NSString *text) {

NSLog(@"callback block : %@", text);

});

const char *types = sel_getName(@selector(testMethod:));

class_replaceMethod([TestObject class], @selector(testMethod:), function, types);

TestObject *object = [[TestObject alloc] init];

[object testMethod:@"lxz"];

callback block : lxz

你可能感兴趣的:(Runtime 实现hook方法)