Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Develoment: Why is my NSURLConnection failing with a "bad URL" error for only some users?

I have an iOS app that requests JSON data from my Rails 3 app, hosted on Heroku, and it works great on my device and for many other users, except one. I have one user who has told me that my app fails to retrieve the JSON data, so I had her send me some log data and the log showed the NSURLConnection delegate method didFailWithError is being called and the error description reads "bad URL". Why is this error occurring and why is it ONLY occurring on just some devices instead of all devices?

Here's my code,

-(void)getTournamentInfoWithUsername:(NSString*)username
{
    NSString *urlString = [NSString stringWithFormat:@"http://myapp-tourney.heroku.com/tournaments/next.json?username=%@", username];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
    [self setUrlConnection:[[NSURLConnection alloc] initWithRequest:request delegate:self]];
}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    [MyLog logError:[NSString stringWithFormat:@"%@ - %@ - %@ - %@", [error localizedDescription], [error localizedFailureReason], [error localizedRecoveryOptions], [error localizedRecoverySuggestion]]];
}

and the log shows...

bad URL - (null) - (null) - (null)

Thanks so much for all your wisdom!

like image 749
BeachRunnerFred Avatar asked May 28 '11 15:05

BeachRunnerFred


2 Answers

It doesn't look like you are encoding the username. Are there spaces or other special characters in the username? Look into using the NSString method stringByAddingPercentEscapesUsingEncoding:.

like image 86
Jim Avatar answered Oct 21 '22 10:10

Jim


It is likely that this depends on the specific URL containing characters that are not allowed for a URL.

You can try and escape the URL:

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding` 
like image 30
sergio Avatar answered Oct 21 '22 09:10

sergio