Can hook +[NSURLSession sessionWithConfiguration:d

2019-09-16 22:26发布

This is baffling me. I have hooked class methods on NSURLConnection with no problems but I am stuck with +[NSURLSession sessionWithConfiguration:delegate:delegateQueue:].

I even tried logging all the class methods with class_copyMethodList (object_getClass([NSURLSession class]), &count); and the class method is actually there: sessionWithConfiguration:delegate:delegateQueue: initialize

And the weird thing is the hook does get called so I think we got it right. Calling %orig() and just passing the parameters down yields:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSURLSession sessionWithConfiguration:delegate:delegateQueue:]: unrecognized selector sent to class 0x1919932b8'

Here's the hook:

+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration
                                  delegate:(id<NSURLSessionDelegate>)delegate
                             delegateQueue:(NSOperationQueue *)queue
{

    NSURLSession *origResult = %orig(configuration, delegate, queue);

    return origResult;
}

Am I missing anything?

Setup details: rpetrich's Theos Mac OS X 10.9.5 iPad Air 1 iOS 7.1.2

2条回答
Explosion°爆炸
2楼-- · 2019-09-16 23:06

The problem here is related to NSURLSession being a class cluster. While the code in OP successfully hooked the class method, %orig needs to call on the real class name. So to make it work, this hook has to be placed under %hook __NSCFURLSession. The real class name might be different for your case.

查看更多
我命由我不由天
3楼-- · 2019-09-16 23:08

Try this code...

+ (NSURLSession *) sessionWithConfiguration: (NSURLSessionConfiguration *) configuration delegate: (id) delegate delegateQueue: (NSOperationQueue *) queue
{
     NSURLSession *  session  = [ NSURLSession  SessionWithConfiguration : Configuraion  
                            delegate : self 
                       DelegateQueue : nil ];
}

delegateQueue Specifies the NSOperationQueue. if your code requires delegateQueue instead of nil you should declare queue.

查看更多
登录 后发表回答