Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to hooking a class method? #157

Open
sayHelloox opened this issue Jan 14, 2019 · 4 comments
Open

how to hooking a class method? #157

sayHelloox opened this issue Jan 14, 2019 · 4 comments

Comments

@sayHelloox
Copy link

sayHelloox commented Jan 14, 2019

I find some answer like #20 #27, but I found those can not work, do someone have other solution, thanks!

@Assuner-Lee
Copy link

Assuner-Lee commented Jan 18, 2019

id object = object_getClass(obj);
[object aspect_...]

@sayHelloox
Copy link
Author

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

@Assuner-Lee
Copy link

id class = object_getClass(obj);
id metaClass = object_getClass(class);
[metaClass aspect_...]

@shinyYangYang
Copy link

  1. import <objc/runtime.h>

  2. fetch MetaClass Object

Class settingsMetal = objc_getMetaClass(NSStringFromClass(Settings.class).UTF8String);

  1. hook with the MetaClass Object

    NSError *error = nil;
    [settingsMetal aspect_hookSelector:@selector(hello:) withOptions:AspectPositionBefore usingBlock:^(id info, NSString *hello){

    } error:&error];
    NSLog(@"error %@", error);

Hope this help you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants