0

I know the same kind of issue has already been posted, but I think I face another one.

I created the CocoaPod called NSURLSession+PromiseKit to allow using PromiseKit with NSURLSession.

I have the definition:

@implementation NSURLSession (PromiseKit)

- (PMKPromise *)promiseDataTaskWithURL:(NSURL *)url {
    return [PMKPromise ...];
}

@end

And the code sample:

NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"..."];
[session promiseDataTaskWithURL:url].then( ^(NSData *data) {
    NSLog(@"Result: %@", data);
}).catch( ^(NSError *e) {
    NSLog(@"Error: %@", e);
});

works on iOS 8, but not on iOS 7. I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFURLSession promiseDataTaskWithURL:]: unrecognized selector sent to instance 0x7c236720'

Now of course I though that the category was not loaded so I added this line to the podspec to make sure that the code is loaded:

s.xcconfig = { 'OTHER_LDFLAGS' => '-ObjC -all_load' }

But it does not work better.

So I added a line to the category file:

+ (void)load {
    NSLog(@"loaded");
}

To my surprise the load method is called, which means that the code is indeed loaded. So why is the category method not found while executing if the code is loaded?

Community
  • 1
  • 1
Jonas Schmid
  • 5,360
  • 6
  • 38
  • 60

1 Answers1

1

Ok, as often, posting the question lead me to the answer.

It has been found that NSURLSession does not support categories.

  • In iOS 7, the underlying NSURLSession class is __NSCFURLSession, which does not support the categories.
  • In iOS 8, the underlying class is __NSURLSessionLocal, which does support categories.

My solution, although cringeworthy is to replace:

@implementation NSURLSession (PromiseKit)

By:

@implementation NSObject (PromiseKit)

Only on the implementation file. This will prevent a Pod user to mistakenly use the method with something else than a NSURLSession object. However, it does not prevent him to play other tricks to make the thing crash...

Community
  • 1
  • 1
Jonas Schmid
  • 5,360
  • 6
  • 38
  • 60