- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个NSURLConnection
和NSURLSession
类别来进行细化,因此我将在运行时拦截 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);
}];
}
关于ios - NSURLConnection的困惑sendAsynchronous:request:queue:completionHandler和sendSynchronousRequest:returningResponse:error:不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216803/
我有一个程序可以打开一个窗口并快速改变背景颜色并随机弹出矩形和椭圆形。我的代码有效,但我不知道为什么,因为我没有在我的代码中调用 repaint() 函数。当我使用我个人的 update() 函数包含
var allRapidSpells = $$('input[value^=RSW]'); 谁能告诉我这是做什么的? 最佳答案 我敢猜测您正在使用 MooTools ,一个 JavaScript 框架
我有一个抽象父类,它有多个子类。我希望 child 能够拥有一个对于该 child 的每个实例都相同的变量。我不想将构造函数传递给 child 来告诉它它的名字,因为当它可以被硬编码时,这看起来很愚蠢
我刚刚在 Git 存储库上做了一些糟糕的事情,我不知道如何解决这个问题。我什至不知道我是怎么把它弄成这样的……! 在存储库(托管在 git hub 上)上,有 3 个我感兴趣的分支:master、br
我是 GIT 的新手,在理解提交日志图时遇到问题。 我感觉每条平行线都是一个分支。虽然我的源代码只有 2 个分支。我在下面提供的提交日志图中看到 3-4 条平行线(Microsoft Team Ser
我是 WPF 的新手,ScrollViewer 让我很沮丧。要么我只是没有“得到”它,要么它是一种有限的控制。 这是我的挫折: 水平滚动错误 水平滚动条仅在列表底部可见(我必须滚动到底部才能看到) 坏
那么 $('table.selectable td.capable input:text') 比 $('table.selectable td input:text') 更好吗?换句话说,指定一个类会
我刚刚完成了计算机图形学类(class),我们必须对光线追踪器进行编程。尽管所有结果都是正确的,但我对 OpenMP 的使用感到困惑(顺便说一句,这不是类(class)的一部分)。我有这个循环(C++
与 PatternSynonyms ( explicitly bidirectional form ),pattern-to-expr 方程实际上形成了一个函数,但拼写为大写(假设您最终得到正确类型的
我是 javascript/coffeescript 新手。 有人可以解释一下为什么这个 CoffeeScript/JavaScript 会毫无延迟地快速通过吗?我对第一种情况的想法是,它是对 upd
如果我调用document.getElementsByClassName('cl'),我会得到一个 HTMLCollection。它似乎包含 Element 对象而不是 HTMLElement 对象,
这是我本月的 azure payasyougo 使用费用。 我很难理解为什么我要为标准中型应用服务付费,我认为它会包含在计算时间中?我只运行一个云服务,这对于一个没有做太多事情的云服务来说似乎有点陡峭
除了the issue I am already having之外,我还在I saw a video on it之后安装了HBase(尚未安装)之前,还安装了Zookeeper。在安装它时,我遇到了许
我正在将 XSLT 与 regexp:match exslt 函数一起使用。上述函数采用 JavaScript Regex 模式。因此,我尝试匹配一组数字 1 到 3 OR 5 到 7 OR 9 到
我想知道为什么这段代码会给出消息:SyntaxError:意外的标记其他。 var compare = function(choice1,choice2){ if(choice1===choice2)
我尝试使用复选框和 JQuery 过滤日历上的事件, $(document).ready(function () { $('.scrollable-menu :checkbox').click(f
假设我们有一个用户想要一个名为:“test/lasdhjal.txt”,无论如何。现在,如果我将其放入新的文件(输入)中;对象里面,它会认为 test/是一个文件夹,而它是名称的一部分。我能做什么呢?
问题是 stash 的更改不会留在我 stash 它们的分支中。其他分支存储将被覆盖示例: 我愿意: git checkout iss4 // made some changes gi
我是一个 java 新手,并且在 StackOverflow 错误/在类之间访问文件的能力方面遇到了一个非常令人困惑的问题。我知道根本原因可能是我进行了一些递归调用,但修复它的语法却让我无法理解。我认
public X createData(int n) { int[] values = new int[n]; Random rand = new Random(); for
我是一名优秀的程序员,十分优秀!