AFNetworking - Invalid parameter not satisfying: url - ios

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.

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

I just had this error, the cause was a space at the start of the url:
http://host.com/etc
^

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

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.

Hope its Working.. In AFnetworing class name AFURLRequestSerialization.m
commented the line 227 : // NSParameterAssert(url);

Related

How to pass GET request for PayTm transaction in ios Objective C

I am integrating PayTm with my app and I want to pass the parameters using GET method.
My code is as follows:
NSString *urlString = [NSString stringWithFormat:#"https://secure.paytm.in/oltp/HANDLER_INTERNAL/TXNSTATUS?JsonData={%22MID%22:%22%#%22,%22ORDERID%22:%22a84afd6c-0e54-42df-b29a-2b057f9e7c53%22}",MIDValue];
where MIDValue is a string.
When I use this code I'm getting error message.
Please give a suggestion to remove the error.
Thank You
Maybe your variable MIDValue contains space and/or &.
You first have to encode it as URL and then pass it as parameter.
Visit this for details, I guess this is what you are looking for
NSString *newParam = [MIDValue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]
NSString *urlString = [NSString stringWithFormat:#"https://secure.paytm.in/oltp/HANDLER_INTERNAL/TXNSTATUS?JsonData={%22MID%22:%22%#%22,%22ORDERID%22:%22a84afd6c-0e54-42df-b29a-2b057f9e7c53%22}", newParam];

NSURl urlWithString replaces body with dots

I try to convert NSString to NSURL however I receive truncated url.
NSURL *url = [NSURL fileURLWithPath:music.url];
If I make NSLog music.url I get:
/Users/Nikita/Library/Developer/CoreSimulator/Devices/D93FE148-7F72-43BB-A545-06D76C71300B/data/Containers/Data/Application/48BC6629-F054-4679-AC7B-709815EC5E2A/Library/Caches/4219609-357670045.mp3
And when NSLog received url:
/Users/Nikita/Library/Developer/CoreSimulator/Devices/D93FE148-7F72-43BB-A545-06D76C71300B/data/Containers/Data/Applicat ... 0045.mp3
The link is partially truncated.
What is the reason?
Thank you
music.ulr is obviously an instance of NSString. url is an instance of NSURL. Using NSLog() an instance of a class makes its own decision how to be logged.
Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
NSLog() prints only a description, not the complete object, whatever this should be.
Obviously the developer of NSString thought, that it is a good idea to print out the complete string and wrote a description method doing that. The developer of NSURLthought that it is enough to print the string partially, so you can recognize it.

%2C URL causing iOS app crash

I have an iOS application which downloads a JSON feed from this URL:
https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%#
I am storing the URL in a NSString for later use. I am also adding a NSString to the end of the URL which contains an access token which I am using for OAuth Authentication (hence the %# at the very end of the URL).
Here is how I am storing the URL:
NSString *pre_yt_user_url = [NSString stringWithFormat:#"https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%#", token_youtube];
As you can see part of the URL has a %2C
This is causing a warning and making my iOS app to crash!!
Here are the warning I get:
Format specifies type 'unsigned-short' but the argument has type NSString
and:
More % conversions than data arguments
What am I doing wrong here? Can't I store a URL in a string??
Thanks, Dan.
When using stringWithFormat the % character is the start of a data argument unless it's escaped. So you need to escape it because you don't want to use it as a supplied parameter. You need to use %%2C (because the first % escapes the second %).

How to resolve a NULL cString crash

I'm getting a crash with the following encoding fix I'm trying to implement:
// encoding fix
NSString *correctStringTitle = [NSString stringWithCString:[[item objectForKey:#"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
cell.titleLabel.text = [correctStringTitle capitalizedString];
my crash log output states:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithCString:encoding:]: NULL cString'
thanks for any help
You seem to think that a double conversion is necessary in order to get the correct result. It isn't, nor is it possible (that's why the call to cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL). Just leave this part out and assign to correctStringTitle directly.
I had the same problem with italian accented characters while trying to get text from html and my solution to avoid the situation that cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL is ensuring that when you get the html from data you use:
[[NSString alloc] initWithData:self.responseData encoding:NSISOLatin1StringEncoding]
and not
encoding:NSUTF8StringEncoding

'NSInvalidArgumentException', reason: 'data parameter is nil'

XCode newbie here and i have been trying to figure out why the following is happening...
With the way the code is currently written, i keep getting 'NSInvalidArgumentException'.
reason: 'data parameter is nil' in xcode. The url works fine on the browser.
When I remove the "filters=%7B%22region%22%3A%22CA%22%7D" part of the url, it works fine in Xcode, but when this section of the url is included, that's when I get the error message. I have tried using \" in replacement of the %22 but still nothing. Any suggestions are greatly appreciated.
NSURL *url = [NSURL URLWithString:[#"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL: url];
I also had the same problem:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
But I had solved it. The reason is my API URL is incorrect. Please check out your API URL. Good luck for you!
The code you have posted works without crashing, but the data object ends up being nil
At some point later in the code you access data. Since data == nil, the app crashes.
I suggest entering
http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi
and
http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi
into a browser to shed some light on the situation
UPDATE
The problem was encoding already encoded strings. (The %'s are being encoded to %25)
NSString *urlBase = #"http://api.v3.factual.com/t/restaurants-us?";
NSString *urlData = [#"q=peets+coffee&filters={\"region\":\"CA\"}&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",urlBase,urlData]];
NSData* data = [NSData dataWithContentsOfURL: url];
Works, because you are encoding a non-encoded string.
OR
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"]];
NSData* data = [NSData dataWithContentsOfURL: url];
Will work because as your post is, your string was already encoded properly
I had the same issue and I found one word in service name in upper case while actually it was in lower on server. I did fixed the name in URL and issue got resolved. Thanks.
I think the app crash at offline mode and you tried to NSJSONSerialization the data. Thanks
In my case, I forgot to add App Transport Security Settings in the .plist.
So just go to the .plist and add App Transport Security Settings, and then Allow Arbitrary Loads to yes.
I had the same problem. I found out that error was produced because the URL I was using to download some stuff using NSURLSession had become invalid. Removing the operation solved the problem for me.

Resources