Can hook +[NSURLSession sessionWithConfiguration:delegate:delegateQueue:] but calling %orig gives “unrecognized selector”

北城以北 提交于 2019-12-12 01:59:45

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/26602286/can-hook-nsurlsession-sessionwithconfigurationdelegatedelegatequeue-but-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!