Skip to content

使用NSProxy的时候遇到问题了 #11

Open
@hcl416029105

Description

@hcl416029105

我在使用NSProxy替换NSURLSession原来的的delegate的时候,再使用AFNetwork的AFHTTPSessionManager的GET方法的时候崩溃了,不知道是什么原因,具体代码如下:

static void swizzleClassMethod(Class theClass, SEL originalSelector, SEL swizzledSelector)
{
Method origMethod = class_getClassMethod(theClass, originalSelector);
Method newMethod = class_getClassMethod(theClass, swizzledSelector);

theClass = object_getClass((id)theClass);

if(class_addMethod(theClass, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
    class_replaceMethod(theClass, swizzledSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
    method_exchangeImplementations(origMethod, newMethod);

}

@interface NSURLSession()

@Property(nonatomic, strong) NSURLDelegateProxy *delegateProxy;

@EnD;

@implementation NSURLSession (DelegateMonitor)

  • (void)load {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    swizzleClassMethod([self class],
    @selector(sessionWithConfiguration:delegate:delegateQueue:),
    @selector(hcz_sessionWithConfiguration:delegate:delegateQueue:));
    });

}

  • (NSURLSession *)hcz_sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(nullable id )delegate delegateQueue:(nullable NSOperationQueue *)queue
    {
    if (delegate) {
    NSURLDelegateProxy *proxy = [[NSURLDelegateProxy alloc] initWithTarget:delegate];
    NSURLSession *session = [NSURLSession hcz_sessionWithConfiguration:configuration delegate:proxy delegateQueue:queue];

      session.delegateProxy = proxy;
      
      return session;
    

    }

    return [self hcz_sessionWithConfiguration:configuration delegate:delegate delegateQueue:queue];
    }

@EnD

@interface NSURLDelegateProxy()
@Property(nonatomic, weak) id proxyTarget;

@EnD

@implementation NSURLDelegateProxy

  • (instancetype)initWithTarget:(id)target
    {
    self.proxyTarget = target;
    return self;
    }

  • (void)forwardInvocation:(NSInvocation *)invocation
    {
    SEL sel = [invocation selector];
    if ( [self.proxyTarget respondsToSelector:sel]) {
    [invocation invokeWithTarget:self.proxyTarget];
    }

}

  • (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel
    {

    NSMethodSignature *methodSignature = nil;

    if ( [self.proxyTarget respondsToSelector:sel]) {
    methodSignature = [self.proxyTarget methodSignatureForSelector:sel];
    }

    return methodSignature;
    }

都是很简单的调用,AFHTTPSessionManager的调用方法如下:

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    NSURLSessionDataTask *task = [session GET:@"https://hcz-static.pingan.com.cn/fuelCard/fuelCard.zip" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

错误提示:
2017-12-08 18:08:44.923871+0800 AFNetworkingDemo[82876:14788953] *** NSForwarding: warning: object 0x608000036560 of class '__NSMessageBuilder' does not implement doesNotRecognizeSelector: -- abort

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions