如何用theos钩住NSURLSession方法? [英] How to hook NSURLSession methods with theos?
问题描述
我使用 rpetrich的theos 创建了一个调整项目,并想钩住NSURLSession方法,但是钩子没有似乎被调用了?为什么?这是我的Tweak.xm代码:
I created a tweak project using rpetrich's theos and wanted to hook NSURLSession methods but the hooks don't seem to get invoked? Why? This is my Tweak.xm code:
%hook NSURLSession
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
{
NSLog(@"testhook dataTaskWithRequest:completionHandler:");
return %orig(request, completionHandler);
}
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
{
NSLog(@"testhook dataTaskWithRequest");
return %orig(request);
}
%end
%hook NSMutableURLRequest
+ (id)requestWithURL:(NSURL *)URL
{
NSLog(@"testhook NSMutableURLRequest");
return %orig(URL);
}
%end
我添加了NSMutableURLRequest
钩子,以确保已加载文件和整个调整.我可以验证它确实钩住了requestWithURL:
,但没有钩住任何NSURLSession方法.我正在针对 NSURLSessionExample 中的代码进行测试.
I added the NSMutableURLRequest
hook to make sure that the file and the whole tweak was being loaded. I can verify that it does hook requestWithURL:
but not any of the NSURLSession methods. I am testing against the code from NSURLSessionExample.
这里缺少什么?有人成功钩住了NSURLSession吗?
What's missing here? Has anybody successfully hooked NSURLSession?
推荐答案
NSURLSession
是一个类集群,您将钩住不包含(或很少有)代码的顶级类.
NSURLSession
is a class cluster, and you are hooking the toplevel class that contains no (or scarce little) code.
您应该研究NSURLSession
的子类,有可能通过就地记录NSURLSession
对象的真实类来进行研究.在有限的测试中,我收到了一个对象,该对象的类实际上名为__NSURLSessionLocal
.
You should investigate the subclasses of NSURLSession
—potentially by logging the real class of an NSURLSession
object in-situ. In my limited testing, I received an object whose class was truly named __NSURLSessionLocal
.
这篇关于如何用theos钩住NSURLSession方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!