arlg
arlg

Reputation: 1138

AFNetworking - Invalid parameter not satisfying: url

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.

Upvotes: 18

Views: 25476

Answers (5)

Amr Angry
Amr Angry

Reputation: 3831

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]];

Upvotes: 2

yogesh wadhwa
yogesh wadhwa

Reputation: 721

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

commented the line 227 : // NSParameterAssert(url);

Upvotes: 0

ArturOlszak
ArturOlszak

Reputation: 2663

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.

Upvotes: 0

James Webster
James Webster

Reputation: 32066

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

 http://host.com/etc
^

Upvotes: 6

Kasper Munck
Kasper Munck

Reputation: 4191

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];

Upvotes: 27

Related Questions