gpt4 book ai didi

ios - NSURLConnection的困惑sendAsynchronous:request:queue:completionHandler和sendSynchronousRequest:returningResponse:error:不起作用

转载 作者:行者123 更新时间:2023-12-01 18:14:15 29 4
gpt4 key购买 nike

我创建了一个NSURLConnectionNSURLSession类别来进行细化,因此我将在运行时拦截 call 并收集网络信息。
除了当我使用NSURLConnection的静态类方法时,大多数情况下一切正常

sendAsynchronousRequest:queue:completionHandler:
sendSynchronousRequest:returningResponse:error:

这两个静态方法只是不遵守我的要求,而在调试时,我看到方法实现交换与我所困惑的其他方法一样正确进行。
这是我的代码,类似于我对其他似乎效果很好的方法所做的工作。
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];
});
}

我不知道,对我来说看起来还不错...您如何看待?

最佳答案

这是我的代码,我能够处理(void)sendAsynchronousRequest:(NSURLRequest *)请求队列:(NSOperationQueue)队列完成处理程序:(void(^)(NSURLResponse,NSData *,NSError *))处理程序

// + (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
void (*gOrigNSURLConnection_sendAsynchronousRequestQueueCompletionHandler)(id,SEL,NSURLRequest *,NSOperationQueue *,DataTaskCompletionBlock) = NULL;


static void NSURLConnection_logTelemetrySendAsynchronousRequestQueueCompletionHandler(id self, SEL _cmd, NSURLRequest* request, NSOperationQueue* queue, DataTaskCompletionBlock completionHandler)
{
NSLog(@"i am catched");
return gOrigNSURLConnection_sendAsynchronousRequestQueueCompletionHandler( self, _cmd ,request,queue,completionHandler);
}

// in swizzling method
// NSURLConnection + (void)sendAsynchronousRequest:queue:completionHandler:

selMethod = @selector(sendAsynchronousRequest:queue:completionHandler:);
impOverrideMethod = (IMP) NSURLConnection_logTelemetrySendAsynchronousRequestQueueCompletionHandler;
origMethod = class_getClassMethod(c,selMethod);
gOrigNSURLConnection_sendAsynchronousRequestQueueCompletionHandler = (void *)method_getImplementation(origMethod);

if( gOrigNSURLConnection_sendAsynchronousRequestQueueCompletionHandler != NULL )
{
method_setImplementation(origMethod, impOverrideMethod);
++numSwizzledMethods;
} else {
NSLog(@"error: unable to swizzle + (void)sendAsynchronousRequest:queue:completionHandler:");
}

我的测试代码
- (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.086 TelemetrySwizzling [3017:60b]我被抓到
  • 2014-06-17 10:31:29.103 TelemetrySwizzling [3017:60b]有一个
  • 关于ios - NSURLConnection的困惑sendAsynchronous:request:queue:completionHandler和sendSynchronousRequest:returningResponse:error:不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216803/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com