Search code examples
iosobjective-cobjective-c-category

Calling method from category gives "Unrecognised selector error" in iOS 7.1, but in 8.4 it's ok


Basicly, question header explains everything. I tried a lot of things, but it doesn't work.

I've got category that extends NSURLSession, that handles server and network errors

- (NSURLSessionDataTask *)
dataTaskHandlingErrorsWithRequest:(NSURLRequest *)request
                completionHandler:(void (^)(NSData *))completionHandler {
  return
      [self dataTaskWithRequest:request
              completionHandler:^(NSData *data, NSURLResponse *response,
                                  NSError *error) {
                if (!error && response &&
                    [response isKindOfClass:[NSHTTPURLResponse class]]) {
                  NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
                  if (resp.statusCode / 100 == 2) {
                    completionHandler(data);
                  } else {
                    // Wrong status code from server.
                    NSNotificationCenter *center =
                        [NSNotificationCenter defaultCenter];
                    [center postNotificationName:kPANotifiNameServerStatusError
                                          object:self
                                        userInfo:@{
                                          @"response" : resp
                                        }];
                    NSLog(@"Wrong status code");
                    completionHandler(nil);
                  }
                } else {
                  // Something wrong with network.
                  NSNotificationCenter *center =
                      [NSNotificationCenter defaultCenter];
                  [center postNotificationName:kPANotifiNameNetworkError
                                        object:self
                                      userInfo:@{
                                        @"error" : error ? error : [NSNull null]
                                      }];
                  NSLog(@"Internet connection problem.");
                  completionHandler(nil);
                }
              }];
}

And here is where I'm calling it from:

- (void)authenticateWithHandler:(AuthHandler)handler {
  NSURLSession *session = [NSURLSession sharedSession];
  NSURLSessionTask *task =
      [session dataTaskHandlingErrorsWithRequest:self.request
                               completionHandler:^(NSData *data) {
                                 if (data) {
                                   BOOL suc = [self handleResponse:data];
                                   handler(suc);
                                 } else {
                                   handler(NO);
                                 }
                               }];
  [task resume];
}

So, on iOS 7.1, if I'm calling that method, it throws

2015-08-06 15:29:50.973 MyApp[2618:607] -[__NSCFURLSession dataTaskHandlingErrorsWithRequest:completionHandler:]: unrecognized selector sent to instance 0x7871bf40
2015-08-06 15:29:50.976 MyApp[2618:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFURLSession dataTaskHandlingErrorsWithRequest:completionHandler:]: unrecognized selector sent to instance 0x7871bf40'

But on iOS 8 it works. I checked compile sources, everything is ok there.


Solution

  • Can you check in your category method:

    if ([self isKindOfClass:[NSURLSession class]]) {
        NSLog(@"YES");
    }
    

    Maybe the object you're trying to run it on is just casted to NSURLSession while in fact it is not..? Just some guesses, as I still have no idea.

    I'd like here to point Mattt answer, which can help to understand how does it work.