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

Trouble hooking a class method #20

Closed
lukestringer90 opened this issue May 13, 2014 · 5 comments
Closed

Trouble hooking a class method #20

lukestringer90 opened this issue May 13, 2014 · 5 comments

Comments

@lukestringer90
Copy link

I'm am trying to swizzle a class method which ordinarily returns a singleton instance of a AFHTTPSessionManager subclass. My class is as follows:

@interface WebServiceClient : AFHTTPSessionManager

+ (instancetype)sharedClient;

@implementation WebServiceClient

+ (instancetype)sharedClient {
    static WebServiceClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = ...; // setup here
    });

    return _sharedManager;
}

@end

I am swizzling like this:

WebServiceClient *webServiceClient = ...; // setup here
NSError *error;
[WebServiceClient aspect_hookSelector:@selector(sharedClient)
                          withOptions:AspectPositionInstead
                           usingBlock:^(id<AspectInfo> info) {
                               NSInvocation *invocation = info.originalInvocation;
                               [invocation setReturnValue:&webServiceClient];
                          } error:&error];

The error gives the following description:

Aspects: Blog signature <NSMethodSignature: 0x8e2bec0> doesn't match (null).

Any ideas what I am doing wrong? Thanks for the great library, I think I am going to be using it a lot in my unit tests to simplify dependency injection.

@steipete
Copy link
Owner

Oh we don't yet support hooking class-methods. Might come in the future !

@lukestringer90
Copy link
Author

That's fine, thanks for the clarification. Keep up the good work :)

steipete added a commit that referenced this issue May 18, 2014
@steipete
Copy link
Owner

Hey I added that feature and made it even cooler! Just return the object instead of fighting with the invocation.

[WebServiceClient aspect_hookSelector:@selector(sharedClient)
                          withOptions:AspectPositionInstead
                           usingBlock:^id {
                                    return webServiceClient;
                              } error:&error];

@lukestringer90
Copy link
Author

Awesome. You sir are a legend!

@steipete
Copy link
Owner

Ha, thanks! Right now it's just on a branch, since we don't yet have 64bit simulator support, but that will come soon, once I prep up my assembly-foo :)

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

2 participants