18

I just tried AFNetworking on ios7 and i get this error:

    /Classes/AFHTTPClient.m:227
 2013-09-16 18:25:57.557 App[13531:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: url

I don't know what's going on, is there an issue with the lib an ios7 ? thanks.

arlg
  • 1,128
  • 3
  • 16
  • 34
  • 1
    I'm struggling with this problem now too. Have you found a solution yet? – Leon Oct 01 '13 at 09:00
  • I commented the line 227 : // NSParameterAssert(url); And it works.. – arlg Oct 10 '13 at 09:12
  • 2
    It would help if you included the code where the program is erroring out. But, you should check if you are passing nil in the URL parameter like: `[[AFHTTPClient alloc] initWithBaseURL:nil]`. The NSParameterAssert(url) is in there to prevent against initializing the HTTP client with a nil URL. – Jamie Forrest Oct 13 '13 at 03:45
  • 1
    @JamieForrest: Eventually I found out that the predefined settings from the Settings.bundle weren't read properly that's why my url was not filled at all. I assumed it was because I defined a default value, but sadly it didn't work. Just make a breakpoint and print the value of your URL there. – Leon Oct 14 '13 at 12:38
  • 1
    @arlg: That's a very stupid solution. The only thing that NSParameterAssert does is checking if the parameter is actually, and correctly, set. So I strongly discourage this solution! – Leon Oct 14 '13 at 12:40

5 Answers5

27

As Leon says in his comment, commenting out NSParameterAssert is not an ideal solution. An assertion has been put there for a reason by the author of AFNetworking. The code not passing the assertion is likely to be caused by an invalid URL.

Remember that the NSURL factory methods (that is, + URLWithString: and its siblings) will return nil when they're passed an invalid URL string. In this context, an invalid string is a string containing an invalid URL.

What you should do instead of commenting our the assertion, is making sure that you are not passing any invalid URL's to your AFHTTPClient instance. This Stackoverflow answers gives an example of how you could validate a URL. Here's a sample from the answer:

- (BOOL)validateUrl:(NSString *)candidate {
    NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    return [urlTest evaluateWithObject:candidate];
}

Alternatively, you could add percentage escapes using NSString's stringByAddingPercentEscapesUsingEncoding method. Like this:

NSString *encoded = [notEncoded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Community
  • 1
  • 1
Kasper Munck
  • 4,173
  • 2
  • 27
  • 50
  • I know It wasn't an ideal solution, but I needed a quick fix. I can't try your answer right now because I'm not working on the project anymore. Anyway, Thanks for the answer, I will try it when I'll work again on the project :). – arlg Feb 10 '14 at 15:08
  • In my case, it turns out I had inadvertently added space at the beginning of the url string. So, yeah, this was mostly about misformed url. – Chen Li Yong Apr 15 '19 at 04:28
6

I just had this error, the cause was a space at the start of the url:

 http://host.com/etc
^
James Webster
  • 31,873
  • 11
  • 70
  • 114
2

as Kasper says --> "You could add percentage escapes using NSString's stringByAddingPercentEscapesUsingEncoding method"

or better use the following since it deprecated in ios 9

path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
0

I had a weird case today with this assert. My problem was that I have copied url from other text editor (Sublime) and that's why url was invalid.

I was not believing until I have tested it few times.

ArturOlszak
  • 2,653
  • 2
  • 21
  • 20
0

Hope its Working.. In AFnetworing class name AFURLRequestSerialization.m

commented the line 227 : // NSParameterAssert(url);
yogesh wadhwa
  • 711
  • 8
  • 16