Aspects
Aspects copied to clipboard
how to hooking a class method?
I find some answer like https://github.com/steipete/Aspects/issues/20 https://github.com/steipete/Aspects/issues/27, but I found those can not work, do someone have other solution, thanks!
id object = object_getClass(obj); [object aspect_...]
can not work for me , What am wrong with me?
id object = object_getClass([TestObject new]);
[object aspect_hookSelector:@selector(testClassMethod) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> info){
NSLog(@"Class method ------ hook");
} error:NULL];
//then call method [TestObject testClassMethod];
// No log "Class method ------ hook" print
id class = object_getClass(obj); id metaClass = object_getClass(class); [metaClass aspect_...]
-
import <objc/runtime.h>
-
fetch MetaClass Object
Class settingsMetal = objc_getMetaClass(NSStringFromClass(Settings.class).UTF8String);
-
hook with the MetaClass Object
NSError *error = nil; [settingsMetal aspect_hookSelector:@selector(hello:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info, NSString *hello){
} error:&error]; NSLog(@"error %@", error);
Hope this help you !