Ios NSURLConnection sendAsynchronous:请求:队列:completionHandler和sendSynchronousRequest:returningResponse:错误:未工作

Ios NSURLConnection sendAsynchronous:请求:队列:completionHandler和sendSynchronousRequest:returningResponse:错误:未工作,ios,objective-c,nsurlconnection,objective-c-runtime,Ios,Objective C,Nsurlconnection,Objective C Runtime,我已经创建了一个要swizzle的NSURLConnection和NSURLSession类别,以便在运行时拦截呼叫并收集网络信息。 除了使用NSURLConnection sendAsynchronousRequest:queue:completionHandler: sendSynchronousRequest:returningResponse:error: 这两个静态方法在我的swizzling中并不遵循,在调试时,我看到方法实现交换与我的swizzling中的其他方法一样正常。 这是

我已经创建了一个要swizzle的
NSURLConnection
NSURLSession
类别,以便在运行时拦截呼叫并收集网络信息。 除了使用
NSURLConnection

sendAsynchronousRequest:queue:completionHandler:
sendSynchronousRequest:returningResponse:error:
这两个静态方法在我的swizzling中并不遵循,在调试时,我看到方法实现交换与我的swizzling中的其他方法一样正常。 这是我的代码,类似于我对其他方法所做的,这些方法似乎工作得很好

typedef void (^SendAsynchronousCompletionHandlerBlock)(NSURLResponse*, NSData*, NSError*);

static void (*OriginalNSURLConnectionSendAsynchronousRequestQueueCompletionHandler)(id, SEL, NSURLRequest*, NSOperationQueue*, SendAsynchronousCompletionHandlerBlock);
static NSData* (*OriginalNSURLConnectionSendSynchronousRequestReturningResponseError)(id, SEL, NSURLRequest*, NSURLResponse**, NSError**);

static void MyNSURLConnectionSendAsynchronousRequestQueueCompletionHandler(id self, SEL _cmd, NSURLRequest* request, NSOperationQueue* queue, SendAsynchronousCompletionHandlerBlock completionHandler)
    {
    NSLog(@"Implementation Intercept in %s", __PRETTY_FUNCTION__);

    OriginalNSURLConnectionSendAsynchronousRequestQueueCompletionHandler(self, _cmd, request, queue, completionHandler);
    }

static NSData* MyNSURLConnectionSendSynchronousRequestReturningResponseError(id self, SEL _cmd, NSURLRequest* request, NSURLResponse** response, NSError** error)
    {
    NSLog(@"Implementation Intercept in %s", __PRETTY_FUNCTION__);

    NSData* data = OriginalNSURLConnectionSendSynchronousRequestReturningResponseError(self, _cmd, request, response, error);

    return data;
    }

 @implementation NSURLConnection (MyNSURLConnection)

+ (void) load
    {
    // Create onceToken
    static dispatch_once_t onceToken;
    // Use dispatch_once to make sure this runs only once in the lifecycle
    dispatch_once(&onceToken,
        ^{
        NSLog(@"Injecting code to NSURLConnection");
        [self injectImplementationToNSURLConnectionSendAsynchronousRequestQueueCompletionHandler];
        [self injectImplementationToNSURLConnectionSendSynchronousRequestReturningResponseError];

        // Some other methods I intercept, just as reference, they work as tested to init an NSURLConnection object
        // I will skip their implementation which is similar to what I show here
        [self injectImplementationToNSURLConnectionConnectionWithRequestDelegate];
        [self injectImplementationToNSURLConnectionInitWithRequestDelegateStartImmediately];
        [self injectImplementationToNSURLConnectionInitWithRequestDelegate];
        [self injectImplementationToNSURLConnectionStart];
        });
    }

+ (void) injectImplementationToNSURLConnectionSendAsynchronousRequestQueueCompletionHandler
    {
    // Replace the method on the same class that's used
    // in the calling code
    Class class =  [NSURLConnection class];

    // The Original +sendAsynchronousRequest:queue:completionHandler:
    SEL originalSelector = @selector(sendAsynchronousRequest:queue:completionHandler:);

    // The Replacement method implementation
    IMP replacement = (IMP)MyNSURLConnectionSendAsynchronousRequestQueueCompletionHandler;

    // This will eventually hold the original sendAsynchronousRequest:queue:completionHandler:
    IMP* store = (IMP*)&OriginalNSURLConnectionSendAsynchronousRequestQueueCompletionHandler;

    IMP originalImp = NULL;
    Method method = class_getClassMethod(class, originalSelector);
    if (method)
        {
        const char* type = method_getTypeEncoding(method);
        // Replace the original method with the MyNSURLConnectionSendAsynchronousRequestQueueCompletionHandler
        originalImp = class_replaceMethod(class, originalSelector, replacement, type);
        if (!originalImp)
            {
            originalImp = method_getImplementation(method);
            }
        }

    // Put the original method IMP into the pointer
    if (originalImp && store)
        {
        *store = originalImp;
        }
    }

+ (void) injectImplementationToNSURLConnectionSendSynchronousRequestReturningResponseError
    {
    // Replace the method on the same class that's used
    // in the calling code
    Class class =  [NSURLConnection class];

    // The Original +sendSynchronousRequest:returningResponse:error: selector
    SEL originalSelector = @selector(sendSynchronousRequest:returningResponse:error:);

    // The Replacement method implementation
    IMP replacement = (IMP)MyNSURLConnectionSendSynchronousRequestReturningResponseError;

    // This will eventually hold the original sendSynchronousRequest:returningResponse:error:
    IMP* store = (IMP*)&OriginalNSURLConnectionSendSynchronousRequestReturningResponseError;

    IMP originalImp = NULL;
    Method method = class_getClassMethod(class, originalSelector);
    if (method)
        {
        const char* type = method_getTypeEncoding(method);
        // Replace the original method with the MyNSURLConnectionSendSynchronousRequestReturningResponseError
        originalImp = class_replaceMethod(class, originalSelector, replacement, type);
        if (!originalImp)
            {
            originalImp = method_getImplementation(method);
            }
        }

    // Put the original method IMP into the pointer
    if (originalImp && store)
        {
        *store = originalImp;
        }
    }
那么,是什么使这些方法不同呢?我不能让我的代码快速转换到原始实现中,而且不会出现任何错误

下面是我测试它的代码:

- (IBAction) executeURLRequest: (UIButton*)sender
    {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://My.URL/file.json"]];
    [request setValue:@"API_KEY" forHTTPHeaderField:@"X-My-Auth-Token"];
    NSURLResponse* response;
    NSError* error;
    // Doesn't work, my swizzle method is not invoked
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    // Doesn't work, my swizzle method is not invoked
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:
        ^(NSURLResponse *response, NSData *data, NSError *error)
        {
        if (error) NSLog(@"NSURLConnection failed: %@", [error debugDescription]);
        NSLog(@"Made the NSURLRequest to My");
        }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
        ^{
        // It works, I get to see my message of the method invoked to the output console
        NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:nil startImmediately:YES];
        });
    }

我不知道,在我看来这很好……你怎么看?

首先,不要在调试和/或学习目的之外使用系统框架方法。它是脆弱的,将突破操作系统的版本,并且在发现时不会批准或删除关闭系统框架的应用程序


其次,最有可能的情况是
NSURLConnection
实现为类集群。因此,可能有一些子类实现了实际的连接,而您正在滑动abstract super的实现,它什么也不做。

首先,不要在调试和/或学习目的之外滑动系统框架方法。它是脆弱的,将突破操作系统的版本,并且在发现时不会批准或删除关闭系统框架的应用程序


其次,最有可能的情况是
NSURLConnection
实现为类集群。因此,可能存在实现实际连接的某个子类,而您正在滑动抽象超级的实现,而抽象超级的实现没有任何作用。

尽管您需要特别小心滑动,但我不知道苹果有任何禁止滑动系统框架的政策。我自己也使用过这些类,SDK已经包含在应用商店的应用程序中

NSURLConnection未实现为类群集,尽管NSURLSession是

我编写了在Apigee iOS SDK中Swizzle NSURLConnection和NSURLSession的代码。在这里查看NSURLConnection swizzling的实现:


尽管你需要特别小心滑动,但我不知道苹果有任何禁止滑动系统框架的政策。我自己也使用过这些类,SDK已经包含在应用商店的应用程序中

NSURLConnection未实现为类群集,尽管NSURLSession是

我编写了在Apigee iOS SDK中Swizzle NSURLConnection和NSURLSession的代码。在这里查看NSURLConnection swizzling的实现:


这是我的代码,我可以快速切换(void)sendAsynchronousRequest:(NSURLRequest*)请求队列:(NSOperationQueue)队列完成处理程序:(void(^)(NSURLResponse,NSData*,NSError*)处理程序

我的测试代码

- (IBAction)clickSendAsyncWithBlock:(id)sender {
    NSURL *URL = [NSURL URLWithString:@"http://localhost:8000/a.txt"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"got %@" , myString);
                           }];
}
结果:

  • [双语新闻]我被抓到了
  • 2014-06-17 10:31:29.103遥测扫描[3017:60b]得到一个

这是我的代码,我可以快速切换(void)sendAsynchronousRequest:(NSURLRequest*)请求队列:(NSOperationQueue)队列完成处理程序:(void(^)(NSURLResponse,NSData*,NSError*)处理程序

我的测试代码

- (IBAction)clickSendAsyncWithBlock:(id)sender {
    NSURL *URL = [NSURL URLWithString:@"http://localhost:8000/a.txt"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"got %@" , myString);
                           }];
}
结果:

  • [双语新闻]我被抓到了
  • 2014-06-17 10:31:29.103遥测扫描[3017:60b]得到一个

你好,保罗,谢谢你的帮助,实施得很好!苹果没有对swizzle的限制,iOS 7和iOS 8 SDK也充满了这种限制。我知道你也不能使用我想要的方法,而是创建了自己的方法包装器供开发人员使用。你知道为什么吗?我已经测试过Apigee框架,我也经历过同样的行为,你正在录制sendSynchronous(我没有这样做是有原因的),但是异步版本也没有从Apigee SDK中录制。嗨,Paul,谢谢你,非常棒的实现!苹果没有对swizzle的限制,iOS 7和iOS 8 SDK也充满了这种限制。我知道你也不能使用我想要的方法,而是创建了自己的方法包装器供开发人员使用。你知道原因了吗?我测试过Apigee框架,我也经历过同样的行为,你正在录制sendSynchronous(我还没有这么做),但异步版本也没有从Apigee SDK中录制。是的,这个实现很有效,我可以看出您的代码和我的代码之间的区别在于,您使用gOrigNSURLConnection\u sendsynchronousrequestqueuecompletionhandler=(void*)方法\u getImplementation(origMethod)获得原始实现;我对同步版本也做了同样的操作,它也可以工作。我将让我的实现的其余部分保持现在的状态,因为其他一切都按预期工作。如果可能,请解释我的知识库。是的,这个实现是有效的,我可以看到您的代码和我的代码之间的区别是,您使用gOrigNSURLConnection\u sendAsynchronousRequestQueueCompletionHandler=(void*)方法\u getImplementation(origMethod)获得原始实现;我对同步版本也做了同样的操作,它也可以工作。我将让我的实现的其余部分保持现在的状态,因为其他一切都按预期工作。如果可能的话,请解释我的知识库。除了调试和实验学习,在我看来,在单元测试中使用框架方法可能是合理的。除了调试和实验学习,在我看来,使用框架方法可能是合理的