1

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

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.

What's missing here? Has anybody successfully hooked NSURLSession?

radj
  • 4,360
  • 7
  • 26
  • 41
  • What is the filter for this tweak? Also: the syntax you are using for hooks is Logos, not Theos, so you ought to label questions with that instead of Theos. – Aehmlo Nov 05 '14 at 13:14
  • @AehmloLxaitn I used `{ Filter = { Bundles = ( "com.ravi.NSURLSessionExample" ); }; }` which is the bundle ID of the test app. The `NSMutableRequest` hook worked so I guess I got the filter right? I added `logos` tag. I put `theos` anyway since `logos` is a subcomponent of `theos`. – radj Nov 05 '14 at 14:10

1 Answers1

2

NSURLSession is a class cluster, and you are hooking the toplevel class that contains no (or scarce little) code.

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.

Dustin Howett
  • 1,635
  • 11
  • 13
  • Thank you! This worked! I will apply this in-situ inspection on the next classes I encounter that I can't hook. On my end it's `__NSCFURLSession`. It's gonna be tricky trying to guess all possible subclass names. – radj Nov 14 '14 at 10:32
  • I have another question related to this. The hook was successful on the class cluster but it failed in calling `%orig` but after putting the hook in the real class name, it worked flawlessly. http://stackoverflow.com/questions/26602286/can-hook-nsurlsession-sessionwithconfigurationdelegatedelegatequeue-but-ca – radj Nov 14 '14 at 10:57